标签:android style blog http java color
下面我们来看下appWidget如何通过广播来更新appWidget上的信息,在AppWidget应用(二)的基础上,需要添加一个自定义的消息,并且在AndriodMainfest上注册;代码如下
- <?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中的几个方法
- @Override
- public void onReceive(Context context, Intent intent) {
-
- String Msg = intent.getAction();
-
- if (Msg.equals(UPDATE_RECEIVE)) {
- System.out.println("----------------onReceive");
-
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.appwidgetlayout);
-
- remoteViews.setTextViewText(R.id.txtapp, "我变-hihi");
- remoteViews.setImageViewResource(R.id.image, R.drawable.local_file);
-
-
- AppWidgetManager appWidgetManager = AppWidgetManager
- .getInstance(context);
-
-
- ComponentName componentName = new ComponentName(context,
- appWidgetActivity.class);
-
-
- appWidgetManager.updateAppWidget(componentName, remoteViews);
- }
-
- else{
- super.onReceive(context, intent);
- }
- }
-
-
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- System.out.println("----------------onUpdate");
-
-
- Intent intent = new Intent();
- intent.setAction(UPDATE_RECEIVE);
-
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
- intent, 0);
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.appwidgetlayout);
-
-
- remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);
-
-
- appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
- }
启动后的效果如图所示:
点击按钮后TextView 和 ImageView都改变了
照旧附上源码
点击打开链接
原文:http://blog.csdn.net/deng0zhaotai/article/details/10414285
AppWidget应用(三)---PendingIntent 之 getBroadcast,布布扣,bubuko.com
AppWidget应用(三)---PendingIntent 之 getBroadcast
标签:android style blog http java color
原文地址:http://www.cnblogs.com/veins/p/3831778.html