开发APP Widget的步骤


1、继承AppWidgetProvider类,

public class MyWidget extends AppWidgetProvider{
    
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds)

public void onDeleted(Context context, int[] appWidgetIds)

public void onEnabled(Context context)

public void onDisabled(Context context)


public void onReceive(Context context, Intent intent)
    
}

一般在onUpdate中进行widget中按键的事件处理,和初始化值。



2、AppWidgetProvider是一个Broadcast,需要在Mainfest.xml中注册。

 
           
               
           
           
  
  

   3、 中的android:resource引用的是Widget的配置文件,需要在res/xml下新建,如果xml目录不存在,也需要新建。

 widget_config文件


    android:minHeight="20dp" 
    android:updatePeriodMillis="1000000"  
    android:previewImage="@drawable/l7"  
    android:initialLayout="@layout/my_widgets_layout" 
    android:resizeMode="horizontal|vertical"  
     android:configure="cn.fly.widgets.MyWidgetsConfigActivity"> 
     
    

设置了updatePeriodMillis后,就算手机处于待机状态,也会唤醒手机去执行更新操作,如果widget更新频繁,这会对手机电池寿命造成影响,所以针对这种情况,可以使用AlarmManager来执行定时更新操作,将AlarmManager的Type设置为 ELAPSED_REALTIME 或者 RTC,就可以保证手机在唤醒的状态下执行更新,updatePeriodMillis则需要设置为0。


在桌面显示的布局文件的跟节点必须是常用布局例如:Framelayout,linearLayout,viewStub等,不能使用自定义布局。


在4.0以后的版本,widget放置到桌面上是可以根据其他的widget或者快捷图标来扩充边距,使添加的widget能够在桌面视觉上达到协调,但是4.0以下的版本需要自己来设置Padding,使widget能和其他桌面图标达到视觉协调。


做完以上的操作,一个简单的widget的就完成了,根据应用需求我们会遇到以下的一些情况。

一、相应widget的按键事件

    因为widget不是运行在自身的应用的进程中,所以是通过以下方式来处理事件。

  RemoteView 进行布局引用。

   PendingIntent  进行事件分发。

//这段代码一般写在onUpdate中,所处理的事情是按button更改Textview的显示
final int N = appWidgetIds.length;
for(int i = 0;i< N;i++){
    Intent intent = new Intent("自定义action用来标示这个intent");
    intent.setClass(context, MyWidgets.class);
    //发送广播,在onReceive()中进行处理PendingIntent也可以启动Activity,也可以//启动Service
    PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 
					0, intent, 0);
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					"widget布局引用");
    //button按键处理
    remoteViews.setOnClickPendingIntent(R.id.btn_click, mPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}

  在onReceive()的处理

String action = intent.getAction();
if(action.equals("比较发送的action")){
    RemoteViews remoteViews  = new RemoteViews(context.getPackageName(),"widget布局引用");
    //更改文本显示
    remoteViews.setTextViewText(R.id.tv, "测试");
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
    ComponentName componentName = new ComponentName(context,MyWidgets.class);
    appWidgetManager.updateAppWidget(componentName, remoteViews); 
		}

二、widget添加上桌面上时的配置页面

这个配置页面是一个activity,所以需要在mainfest.xml中进行配置

1、在启动这个配置页面的时候,需要在oncreate()方法里面写上setResult(RESULT_CANCELED);这样做是为了防止配置页面还没有显示出来,用户就按了返回键,

setResult(RESULT_CANCELED);
super.onCreate(savedInstanceState);

2、需要获取widgetId

Intent intent = getIntent();
Bundle extra = intent.getExtras();
if(extra !=null){
    mAppWidgetId  = extra.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

如果mAppWidgetId为INVALID_APPWIDGET_ID则退出页面显示。

3、当配置页面显示后,用户做完了配置退出页面时,需要将这些配置信息更新到桌面的widget,照着官方给出的文档上来操作的话,只能显示widget但是配置并没有生效,参考了appdemo中的例子和个人调试后总结如下。


在MyWidget中写一个静态方法

 //describe是一个在配置文件中自由输入文本,然后在widget的text上显示,widget是为了做到代码重用,这里只是一个简单的更改文本,如果涉及到图片更改,列表数据改动等,这些改动如果有配置界面的话基本上和onupdate所做的操作一样。
 public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId, String describe) {
   RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
   views.setTextViewText(R.id.tv, describe);
   appWidgetManager.updateAppWidget(appWidgetId, views);
}

在配置界面的退出方法中这样写

private void exits(){
    String describe = et.getText().toString();
    Context context = this;
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    //appWidgetId在进入配置界面的时候已经获取了
    MyWidget.updateAppWidget(context,appWidgetManager,appWidgetId , describe );
    
    //配置完成退出界面
    Intent intent = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(RESULT_OK, intent );
    finish();
}

当时参考文档做时,文档中并没有提到调用updateAppWidget方法后,退出界面需要重新获取view,并改变view的显示数据。所以我只写了appWidgetManager.updateAppWidget(appWidgetId, views)方法。