我在 Android 4.1.1 中的 AppWidget 遇到了一个非常奇怪的问题。我目前正在开发一个音乐播放器应用程序及其小部件。当歌曲更改、播放器启动和停止时,必须更新小部件。它有一个 ListView,必须与应用程序中的播放列表同步。
在 Jelly Bean 之前,一切都运行良好。在我的测试设备从 4.0.3 升级到 4.1.1 后,每当以编程方式强制更新小部件时,Android 消息传递小部件的布局都会设置为我的小部件几秒钟!我还在模拟器中用 4.1 检查了这个案例,效果很好。
我使用那段代码来强制我的小部件更新:
AppWidgetManager man = AppWidgetManager.getInstance(applicationContext);
int[] ids = man.getAppWidgetIds(
new ComponentName(applicationContext, MuzikLargeWidgetProvider.class));
Intent updateIntent = new Intent();
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
applicationContext.sendBroadcast(updateIntent);
这是我的 onUpdate 方法
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.context = context;
this.appWidgetManager = appWidgetManager;
ComponentName thisWidget = new ComponentName(context,
MuzikLargeWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int appWidgetId : allWidgetIds) {
Intent svcIntent = new Intent(context, UpdateService.class);
svcIntent.putExtra(WidgetActions.DATA_WIDGET_ID, appWidgetId);
context.startService(svcIntent);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
我正在使用一个服务 (UpdateService),它是一个静态内部类,用于更新我的小部件:
public static class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public RemoteViews buildUpdate(Context context, int widgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.muzikwidget_large);
return views;
}
@Override
protected void onHandleIntent(Intent intent) {
// Build the widget update
RemoteViews updateViews = buildUpdate(this, intent.getIntExtra(WidgetActions.DATA_WIDGET_ID, 0));
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, MuzikLargeWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
}
buildUpdate 方法还做了一些事情(设置小部件按钮的 Intent 、设置 TextView 等),但它们与我的问题无关。我在 Asus TF 300 TG 平板电脑上遇到此问题(未 root)。
感谢任何帮助。
最佳答案
问题已解决。我修改了用于强制小部件更新的代码 fragment 。这是有效的:
AppWidgetManager man = AppWidgetManager.getInstance(applicationContext);
int[] ids = man.getAppWidgetIds(
new ComponentName(applicationContext, MuzikLargeWidgetProvider.class));
for (int appWidgetId : ids) {
Intent svcIntent = new Intent(applicationContext, UpdateService.class);
svcIntent.putExtra(WidgetActions.DATA_WIDGET_ID, appWidgetId);
applicationContext.startService(svcIntent);
}
不调用Widget的onUpdate方法,直接调用UpdateService。看来有时应该避免 sendBroadcast。
编辑
如果您需要使用广播来更新您的小部件,这似乎是实现这一目标的正确方法:
AppWidgetManager man = AppWidgetManager.getInstance(applicationContext);
int[] ids = man.getAppWidgetIds(
new ComponentName(applicationContext, MuzikLargeWidgetProvider.class));
Intent updateIntent = new Intent(applicationContext, MuzikLargeWidgetProvider.class);
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
applicationContext.sendBroadcast(updateIntent);
关于Android为AppWidget设置不同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798418/