【问题标题】:Android sets different layout to AppWidgetAndroid 为 AppWidget 设置不同的布局
【发布时间】:2012-10-09 10:48:20
【问题描述】:

我的 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 方法做了更多的事情(设置小部件按钮的意图、设置文本视图等),但它们与我的问题无关。我在华硕 TF 300 TG 平板电脑上遇到了这个问题(未植根)。

感谢任何帮助。

【问题讨论】:

    标签: android android-appwidget android-4.2-jelly-bean


    【解决方案1】:

    问题已解决。我修改了用于强制小部件更新的代码片段。这是有效的:

    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);
    

    【讨论】:

    • 谢谢,你帮了我很多。我在 4.1.1 上更新小部件时遇到了问题,而您创建小部件更新意图的方式效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 2013-09-01
    • 2018-10-04
    • 2014-07-30
    • 1970-01-01
    • 2021-10-06
    相关资源
    最近更新 更多