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

来源:互联网 发布:浅猪家的七七知乎 编辑:程序博客网 时间:2024/06/04 21:52
 

思想:利用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")){     
   //处理     
}  

原创粉丝点击