下面介绍下AppWidgetProvider简单的实现过程(我是在AS中创建了一个空的工程,没有Activity)
1.AndroidMainfest文件如下
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<receiver android:name=".MyAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.skywang.widget.UPDATE_ALL"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/my_appwidget_info" />
</receiver>
</application>
创建了类 MyAppWidgetProvider ,还要创建一个layout文件和xml文件
2.layout文件(文件名称为my_appwidget.xml)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="widget" />
<Button
android:id="@+id/my_btm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
一个按钮和一个文本
3.创建xml目录及my_appwidget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="30dp"
android:minHeight="30dp"
android:previewImage="@drawable/preview"
android:initialLayout="@layout/my_appwidget">
</appwidget-provider>
4.实现MyAppWidgetProvider类
public class MyAppWidgetProvider extends AppWidgetProvider {
private static final String TAG = "MyAppWidgetProvider";
private static boolean n = false;
private RemoteViews remoteView = null;
private static final int BUTTON_SHOW = 1;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.e(TAG,"xtk onReceive");
remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);
if(n) {
remoteView.setTextViewText(R.id.my_text, "hahahha");
n = false;
}else{
remoteView.setTextViewText(R.id.my_text, "henhenhen");
n = true;
}
//需要实例化一个pending
remoteView.setOnClickPendingIntent(R.id.my_btm, getPendingIntent(context, BUTTON_SHOW));
}
private PendingIntent getPendingIntent(Context context, int buttonId) {
Intent intent = new Intent();
intent.setClass(context, MyAppWidgetProvider.class);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("custom:" + buttonId));
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0 );
return pi;
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG,"xtk onUpdate");
super.onUpdate(context, appWidgetManager, appWidgetIds);
remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);
appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
}
}
设置小部件信息
remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);
remoteView.setTextViewText(R.id.my_text, “hahahha”);
更新小部件
appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
设置按钮事件
remoteView.setOnClickPendingIntent(R.id.my_btm, getPendingIntent(context, BUTTON_SHOW));
参考学习地址:
1.https://www.cnblogs.com/playing/archive/2011/04/22/2024802.html
2.https://blog.csdn.net/liuqz2009/article/details/50922122
3.https://www.cnblogs.com/itchq/p/3817411.html