gpt4 book ai didi

Android 为 AppWidget 设置不同的布局

转载 作者:行者123 更新时间:2023-11-29 00:35:45 25 4
gpt4 key购买 nike

我的 AppWidget 在 Android 4.1.1 中有一个非常奇怪的问题。我目前正在开发音乐播放器应用程序及其小部件。当歌曲更改、播放器启动和停止时,必须更新小部件。它有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com