更新控件需要通过RemoteView去更新Widget控件内容
例如:
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.example_widget);
rv.setTextColor(R.id.textView1, Color.parseColor("#404040"));
AppWidgetManager wm = AppWidgetManager.getInstance(context);
ComponentName cn = new ComponentName(context,ExampleAppWidgetProvider.class);
wm.updateAppWidget(cn, rv);
看个广播事件和控件更新的完整例子..
新建一个/res/xml文件夹, 放入一个xml文件example_appwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="250px" android:minHeight="50px" android:updatePeriodMillis="80000" android:initialLayout="@layout/example_widget">
</appwidget-provider>
在/res/layout中新建一个xml文件example_appwidget.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/text1"></TextView>
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Button"></Button>
</LinearLayout>
新建一个ExampleAppWidgetProvider.java
package com.cn;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
static String UPDATE_ACTION = "com.cn.ActionInfo";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
//接收广播事件, getAction为广播ACTION, 例如这为UPDATE_ACTION
System.out.println("Action:"+intent.getAction());
if(intent.getAction().equals(UPDATE_ACTION)){
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.example_widget);
rv.setTextColor(R.id.textView1, Color.parseColor("#404040"));
AppWidgetManager wm = AppWidgetManager.getInstance(context);
ComponentName cn = new ComponentName(context,ExampleAppWidgetProvider.class);
wm.updateAppWidget(cn, rv);
}else{
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
System.out.println(appWidgetIds);
Intent i = new Intent();
i.setAction(UPDATE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.example_widget);
rv.setOnClickPendingIntent(R.id.button1, pi);
appWidgetManager.updateAppWidget(appWidgetIds, rv);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cn"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Appwidget3Activity"
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="ExampleAppWidgetProvider">
<!-- 自己需要广播的事件 -->
<intent-filter>
<action android:name="com.cn.ActionInfo"></action>
</intent-filter>
<!-- 该广播事件是发布到Widget, 通知更新插件 -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget"></meta-data>
</receiver>
</application>
</manifest>
PS: 该例子将展现如何在Widget广播一个事件, 通过按钮触发事件进行广播, 通过onReceiver进行接收广播的信息