AppWidget應用(三)---PendingIntent 之 getBroadcast

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

下面我們來看下appWidget如何通過廣播來更新appWidget上的資訊,在AppWidget應用(二)的基礎上,需要添加一個自訂的訊息,並且在AndriodMainfest上註冊;代碼如下

 

[html] view plaincopy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.appwidgetdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.appwidgetdemo.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.   
  26.         <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >  
  27.             <intent-filter>  
  28.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" >  
  29.                 </action>  
  30.             </intent-filter>  
  31.   
  32.             <meta-data  
  33.                 android:name="android.appwidget.provider"  
  34.                 android:resource="@xml/appwidget01" />  
  35.   
  36.             <!-- 註冊要處理的訊息 -->  
  37.             <span style="color:#ff0000;"><intent-filter>  
  38.                 <action android:name="com.example.appWidgetUpdate" >  
  39.                 </action>  
  40.             </intent-filter></span>  
  41.         </receiver>  
  42.     </application>  
  43.   
  44. </manifest>  

 

 

廣播訊息定義為:private static final String UPDATE_RECEIVE = "com.example.appWidgetUpdate";


然後重載AppWidgetProvider中的幾個方法

 

[java] view plaincopy 
  1. /** 
  2.      * 接受廣播事件 
  3.      */  
  4.     @Override  
  5.     public void onReceive(Context context, Intent intent) {  
  6.         // TODO Auto-generated method stub  
  7.         String Msg = intent.getAction();  
  8.         //處理我們自訂的訊息  
  9.         if (Msg.equals(UPDATE_RECEIVE)) { // 判斷廣播如果為:com.qlf.appWidgetUpdate才處理  
  10.             System.out.println("----------------onReceive");  
  11.             // 只能通過遠程對象來設定appwidget中的控制項狀態  
  12.             RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
  13.                     R.layout.appwidgetlayout);  
  14.             //改變指定控制項的值  
  15.             remoteViews.setTextViewText(R.id.txtapp, "我變-hihi");  
  16.             remoteViews.setImageViewResource(R.id.image, R.drawable.local_file);  
  17.   
  18.             // 獲得appwidget管理執行個體,用於管理appwidget以便進行更新操作  
  19.             AppWidgetManager appWidgetManager = AppWidgetManager  
  20.                     .getInstance(context);  
  21.   
  22.             // 相當於獲得所有本程式建立的appwidget  
  23.             ComponentName componentName = new ComponentName(context,  
  24.                     appWidgetActivity.class);  
  25.   
  26.             // 更新appwidget  
  27.             appWidgetManager.updateAppWidget(componentName, remoteViews);  
  28.         }  
  29.         //處理系統發送的訊息  
  30.         else{  
  31.             super.onReceive(context, intent);  
  32.         }  
  33.     }  
  34.   
  35.     /** 
  36.      * 到達指定的更新時間或者當使用者向案頭添加AppWidget時被調用 
  37.      */  
  38.     @Override  
  39.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  40.             int[] appWidgetIds) {  
  41.         System.out.println("----------------onUpdate");  
  42.         // TODO Auto-generated method stub  
  43.         // 建立一個Intent對象  
  44.         Intent intent = new Intent();  
  45.         intent.setAction(UPDATE_RECEIVE);  
  46.         // 這裡是getActivity,當然也可以是broadcastReceiver等   發送一個廣播訊息  
  47.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  
  48.                 intent, 0);  
  49.         RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
  50.                 R.layout.appwidgetlayout);  
  51.   
  52.         //為按鈕綁定監聽器  
  53.         remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);  
  54.   
  55.         // 更新Appwidget  
  56.         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);  
  57.     }  


啟動後的效果:

 

點擊按鈕後TextView 和 ImageView都改變了

 

照舊附上源碼

點擊開啟連結

 

原文:http://blog.csdn.net/deng0zhaotai/article/details/10414285

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.