【问题标题】:In Android, how to distinguish between multiple instances of the same widget?在Android中,如何区分同一个小部件的多个实例?
【发布时间】:2013-11-11 16:31:06
【问题描述】:

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

【问题讨论】:

  • 我也试过了,得出的结论是不可能的。

标签: android


【解决方案1】:

您的每个小部件都由一个特定的 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);
}

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

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2014-08-16
    • 1970-01-01
    • 2022-11-30
    • 2012-03-09
    • 1970-01-01
    • 2011-04-29
    • 2018-02-28
    相关资源
    最近更新 更多