0

I'm making a widget. It will random a number when clicked. When I add multiple of this widget on home screen, they start to generate random number at the same time when clicked. What I want is when a widget is clicked it only updates itself and non of the others around it. Please, help me. My code: class UpdateWidgetService

@Override
public void onStart(Intent intent, int startId) {
    // create some random data
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    for (int widgetId : allWidgetIds) {
        // create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));
        // Set the text
        remoteViews.setTextViewText(R.id.update, "Random: " + String.valueOf(number));

        // Register an onClickListener
        Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class);
        clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetId);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), widgetId, clickIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.btn_update, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();
    super.onStart(intent, startId);
}

class MyWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.w(LOG, "onUpdate method called");
    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    // Update the widgets via the service
    context.startService(intent);
}
4

1 回答 1

0

您的每个小部件都由特定的 ID 标识。您的UpdateWidgetService课程看起来像是正确地循环了 ID。

问题在于你的MyWidgetProvider.onUpdate()方法。这段代码是错误的:

// Get all ids
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

该评论解释了为什么它是错误的 - 它获取所有小部件 ID。


查看onUpdate()方法中的参数:

  • public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)

在这里,您可以看到为您提供了一组小部件 ID,您必须将此数组传递给您的更新服务:

  • int[] appWidgetIds

所以你的代码最终应该看起来像这样:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.w(LOG, "onUpdate method called");

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    // Update the widgets via the service
    context.startService(intent);
}

这样,您只需更新系统发送给您的小部件,而不是每次都更新所有小部件。

于 2014-05-16T14:50:51.510 回答