1,一个类,和2个xml
1234//一个实现AppWidgetProvider的类
public
class
TomAppWidgetProvider
extends
AppWidgetProvider {
}
<!--放在 res/xml 文件夹中命名为tom_appwidget_info.xml--><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/tom_appwidget" android:configure="kg.tom.AppWidgetConfigure" ></appwidget-provider><!-- android:initialLayout: 初始化你的appWidget布局 -->
<!--放置在res/layout/tom_appwidget_provider.xml--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="294dp" android:layout_height="72dp" android:orientation="vertical" ><TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000"/> <Button android:id="@+id/appwidget_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@android:string/ok" /></LinearLayout>在AndroidManifest.xml中声明你的appWidget
<receiver android:name="TomAppWidgetProvider" > <intent-filter><!--让系统能够设别到你的appWidgetProvider的动作--> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter><!--设置你的appWidget的布局--> <meta-data android:name="android.appwidget.provider" android:resource="@xml/tom_appwidget_info" /></receiver>
长按你的Home 界面,你就会看到你的widget已经在列表当中
但是,现在点击是会出错的,这时候我们需要设置我们的AppWidgetConfigure
在activity中增加一个Intent-filter
<intent-filter> <!-- 让系统能够设别到你的appWidgetProvider的动作 --> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter>public class AppWidgetConfigure extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置widgetId int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; //1,将setResult 设置为 取消,用于取消widget host setResult(RESULT_CANCELED); //2,从Intent 中找到widget 的id Intent intent = getIntent(); Bundle extras = intent.getExtras(); if(extras != null){ mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); } //3,如果获取不到 appWidget id 我们就结束 if(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){ Log.d("app", "获取失败退出!!!"); finish(); } final Context context = AppWidgetConfigure.this; //4,实例化你的 appWidget AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); //5,更新widget Intent resultValue = new Intent(); resultValue.putExtra(appWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish(); }}
2,然后写provider的代码
public class TomAppWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub super.onUpdate(context, appWidgetManager, appWidgetIds); final int N = appWidgetIds.length; Log.d("app", "onUpdate--->Ids==="+String.valueOf(N) ); for(int i=0; i < N; i++){ int appWidgetId = appWidgetIds[i]; updateAppWidget(context, appWidgetManager, appWidgetId); } } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){ Log.d("app", "update---->id" + appWidgetId); //1,widget中的的标题 CharSequence text = "这是我第一个widget"; //2,widget显示用布局,并设置text显示的值 RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.tom_appwidget_provider); views.setTextViewText(R.id.appwidget_text, text); //3,通知widget manager 更新 appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); //删除的时候调用的方法 int appids = appWidgetIds.length; Log.d("app", "onDelete--->" + appids); }}
然后就可以运行了:
顺便附上一张简单原理图:..
只要在加上几行代码的appwidget 就能实现挑战到Activity的功能
1,新建一个HelloAppWidget的activity
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId){ Log.d("app", "update---->id" + appWidgetId); //1,设置显示用标题 CharSequence text = "这是我第一个widget"; //1.1,增加跳转用activity相关 intent Intent intent = new Intent(context, HelloAppWidget.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); //2,如果,没有在xml中声明RemoteViews布局,这里就必须要让其他布局基于RemoteViews RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.tom_appwidget_provider); views.setTextViewText(R.id.appwidget_text, text); //2.1将需要跳转的intent绑定到appWidget button中 views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent); //3,通知widget manager 更新 appWidgetManager.updateAppWidget(appWidgetId, views); }
联系客服