標籤:android style blog http java color
下面我們來看下appWidget如何通過廣播來更新appWidget上的資訊,在AppWidget應用(二)的基礎上,需要添加一個自訂的訊息,並且在AndriodMainfest上註冊;代碼如下
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.appwidgetdemo"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.appwidgetdemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" >
- </action>
- </intent-filter>
-
- <meta-data
- android:name="android.appwidget.provider"
- android:resource="@xml/appwidget01" />
-
- <!-- 註冊要處理的訊息 -->
- <span style="color:#ff0000;"><intent-filter>
- <action android:name="com.example.appWidgetUpdate" >
- </action>
- </intent-filter></span>
- </receiver>
- </application>
-
- </manifest>
廣播訊息定義為:private static final String UPDATE_RECEIVE = "com.example.appWidgetUpdate";
然後重載AppWidgetProvider中的幾個方法
[java] view plaincopy
- /**
- * 接受廣播事件
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- String Msg = intent.getAction();
- //處理我們自訂的訊息
- if (Msg.equals(UPDATE_RECEIVE)) { // 判斷廣播如果為:com.qlf.appWidgetUpdate才處理
- System.out.println("----------------onReceive");
- // 只能通過遠程對象來設定appwidget中的控制項狀態
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.appwidgetlayout);
- //改變指定控制項的值
- remoteViews.setTextViewText(R.id.txtapp, "我變-hihi");
- remoteViews.setImageViewResource(R.id.image, R.drawable.local_file);
-
- // 獲得appwidget管理執行個體,用於管理appwidget以便進行更新操作
- AppWidgetManager appWidgetManager = AppWidgetManager
- .getInstance(context);
-
- // 相當於獲得所有本程式建立的appwidget
- ComponentName componentName = new ComponentName(context,
- appWidgetActivity.class);
-
- // 更新appwidget
- appWidgetManager.updateAppWidget(componentName, remoteViews);
- }
- //處理系統發送的訊息
- else{
- super.onReceive(context, intent);
- }
- }
-
- /**
- * 到達指定的更新時間或者當使用者向案頭添加AppWidget時被調用
- */
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- System.out.println("----------------onUpdate");
- // TODO Auto-generated method stub
- // 建立一個Intent對象
- Intent intent = new Intent();
- intent.setAction(UPDATE_RECEIVE);
- // 這裡是getActivity,當然也可以是broadcastReceiver等 發送一個廣播訊息
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
- intent, 0);
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.appwidgetlayout);
-
- //為按鈕綁定監聽器
- remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
-
- // 更新Appwidget
- appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
- }
啟動後的效果:
點擊按鈕後TextView 和 ImageView都改變了
照舊附上源碼
點擊開啟連結
原文:http://blog.csdn.net/deng0zhaotai/article/details/10414285