Android原生Widget使用步骤

需要创建三个XML文件以及一个Class文件
三个XML文件分别是

  1. Widget布局文件
java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello, Widget!"
        android:textSize="20sp" />
</RelativeLayout>
  1. WidgetInfoProvider文件
java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/widght_layout"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard"
    android:previewImage="@drawable/ic_launcher_background" />
  1. AndroidMainfest文件

3.1 在receiver标签下,添加广播的Action

3.2 在meta-data标签下,表明这个广播接收器是一个原生Widget,并加载原生Widget配置文件

java 复制代码
    <receiver
        android:name=".MyTimeWidget"
        android:exported="true">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="com.ljheee.widget.UPDATE_TIME" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/my_time_widget_info" />
    </receiver>

4.继承AppWidgetProvider(实际上是一个广播接收器)

java 复制代码
public class MyWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        // 在第一个 Widget 实例添加到桌面时调用
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            // 创建 RemoteViews 对象,用于更新 Widget 布局
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widght_layout);
            // 在这里可以对 RemoteViews 进行更多的操作,例如设置文本、图片等
            // 创建一个 Intent,用于点击事件
            Intent intent = new Intent(context, MyWidget.class);
            intent.setAction("com.example.myapp.ACTION_CLICK");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            // 更新 Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        // 在最后一个 Widget 实例从桌面移除时调用
    }

    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        // 当 Widget 被删除时调用
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        // 接收广播消息,可以处理 Widget 的点击等事件
        if (intent.getAction().equals("com.example.myapp.ACTION_CLICK")) {
            // 处理点击事件
            // 可以在此处添加相应的逻辑,如更新 Widget 内容或执行其他操作
        }
    }
}
相关推荐
ii_best28 分钟前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk35 分钟前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
Fireworkitte3 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083163 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT4 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.4 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超4 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice4 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变4 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang4 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#