安卓多个主屏小部件没有更新

3

我正在尝试实现一个带有一些小部件的Android应用程序,可以在主屏幕/锁定屏幕上使用! 目前,当创建小部件时,小部件内容是正确的,但是没有保持更新。

这是我的代码: MyWidgetProvider

public class MyWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {

        Integer[] preferencesIds = MyPreferences.getWidgetIds(context);
        Log.d(MyWidgetProvider.class.getSimpleName(), "In SP there is " + Arrays.toString(preferencesIds));


        if (preferencesIds != null && preferencesIds.length > 0) {
            for (int appWidgetId : preferencesIds) {
                Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from preferences");
                updateWidgetId(context, appWidgetManager, appWidgetId);
            }
        }

        if (appWidgetIds != null && appWidgetIds.length > 0) {
            for (int appWidgetId : appWidgetIds) {
                Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from base");
                updateWidgetId(context, appWidgetManager, appWidgetId);
            }
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + Arrays.toString(appWidgetIds) + "has been deleted");
        for (int i = 0; i < appWidgetIds.length; i++) {
            MyPreferences.removeWidgetId(context, appWidgetIds[i]);
        }
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * Update a widget with a specific id
     *
     * @param context          app context
     * @param appWidgetManager the widget manager
     * @param appWidgetId      the widget id to update
     */
    private void updateWidgetId(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        MyPreferences.addWidgetId(context, appWidgetId);

        Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + appWidgetId + " has been updated");
        RemoteViews rv = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        Intent intent = new Intent(context, StickyWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        rv.setRemoteAdapter(android.R.id.list, intent);
        // Handle click
        Intent activityIntent = new Intent(context, NotesListActivity.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, activityIntent, 0);
        rv.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
        // Return
        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }
}

这个想法是将所有的小部件id保存在应用程序的共享首选项中,这样我就可以通过强制使用一个意图来更新它们。例如,在我的MainActivity中,我有如下代码:

/**
 * Starts an intent that force the service to wake up and update
 * the widgets
 */
private void updateWidgets() {
    Intent intent = new Intent(this, MyWidgetProvider.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
    // since it seems the onUpdate() is only fired on that:
    int[] ids = {R.xml.widget_infos}; // As I store the ids, this is kind of useless ?
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);
}

问题在于,我在logcat中得到了正确的内容(正在更新3个小部件,并打印出它们的ID),但在我的屏幕上只有最后一个小部件被更新。

如果有人有线索或者其他任何信息,那将是很棒的,因为这让我疯狂不已 :/

1个回答

0
在一个活动中,您可以以编程方式检索用户主屏幕/锁定屏幕中安装的应用程序关联小部件的所有ID。
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(application);
final int[] smallAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(application, SmallWidgetProvider.class));
for (int appWidgetId : smallAppWidgetIds) {
  appWidgetManager.updateAppWidget(appWidgetId, 
    SmallWidgetProvider.buildRemoteView(application, appWidgetId, whatEverYouNeed));
}

AppWidgetManager.updateAppWidget(int,RemoteView)需要一个RemoteView,这就是为什么自定义WidgetProvider具有生成此RemoteView的静态方法,而不在运行的SmallWidgetProvider实例中。

请注意,调用AppWidgetManager.updateAppWidget(int, RemoteView)不会触发您的自定义AppWidgetProvideronUpdate()

如果您知道每个小部件将具有完全相同的内容,则可以考虑使用AppWidgetManager.updateAppWidget(int[],RemoteView)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接