您的位置:首页 > 移动开发

android 触发AppWidget上控件事件来更新AppWidget

2011-08-18 10:30 441 查看
思想:利用AppWidgetProvider里面的onReceive()事件接收广播来更新AppWidget。

步骤:

一、给AppWidget上的某个控件设置OnClickPendingIntent():

Intent UPintent=new Intent("zyf.test.widget.UP");

PendingIntent pendingIntentUp=PendingIntent.getBroadcast(context, 0, UPintent, 0);

views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);

Intent UPintent=new Intent("zyf.test.widget.UP");

PendingIntent pendingIntentUp=PendingIntent.getBroadcast(context, 0, UPintent, 0);

views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);

二、设置AppWidget的intent-filter:

<receiver android:name="AppWidget">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>

<action android:name="zyf.test.widget.UP"></action>

<action android:name="zyf.test.widget.DOWN"></action>

</intent-filter>

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

</receiver>

<receiver android:name="AppWidget">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>

<action android:name="zyf.test.widget.UP"></action>

<action android:name="zyf.test.widget.DOWN"></action>

</intent-filter>

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

</receiver>

三、在onReceive()中判断;

super.onReceive(context, intent);

if(intent.getAction().equals("zyf.test.widget.UP")){

//处理

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: