在Android中,如何区分同一个小部件的多个实例?

时间:2013-11-11 16:31:06

标签: android

我正在制作一个小部件。单击时会随机输入一个数字。当我在主屏幕上添加多个此小部件时,它们会在单击时同时开始生成随机数。我想要的是当点击一个小部件时它只更新自己而不是周围的其他小部件。请帮我。 我的代码:类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);
}

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

1 个答案:

答案 0 :(得分: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);
}

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