Android AppWidget和IntentService,来自两个不同按钮的结果相同

时间:2017-01-10 22:01:59

标签: android intentservice android-appwidget

Android新手在这里。我有一个带有一个活动的应用程序和一个AppWidget。它们都向IntentService发送命令。活动工作正常,但AppWidget由两个按钮组成,无论我点击哪个按钮,都会得到相同的结果。 也许我不能像AppWidget那样使用IntentService ......?我只是在学习。任何帮助将不胜感激。

我的IntentService类:

public class RemoteIntentService extends IntentService {
    private static final String TAG = "RemoteIntentService";

    public RemoteIntentService() {
        super("RemoteIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle data = intent.getExtras();
        String command = data.getString("command");
        Log.d(TAG, "onHandleIntent: command = " + command);
    }
}

我的AppWidget类:

public class RemoteWidget extends AppWidgetProvider {

    private static final String TAG = "RemoteWidget";

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.remote_widget);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }



    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        for (int appWidgetId : appWidgetIds) {

            Intent intentButton1 = new Intent(context, RemoteIntentService.class);
            intentButton1.putExtra("command", "Button1");

            Intent intentButton2 = new Intent(context, RemoteIntentService.class);
            intentButton2.putExtra("command", "Button2");

            PendingIntent pendingButton1 = PendingIntent.getService(context, 0, intentButton1, 0);
            PendingIntent pendingButton2 = PendingIntent.getService(context, 0, intentButton2, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.remote_widget);

            views.setOnClickPendingIntent(R.id.button1, pendingButton1);
            views.setOnClickPendingIntent(R.id.button2, pendingButton2);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

单击AppWidget上的两个按钮时的输出:

D/RemoteIntentService: onHandleIntent: command = Button1
D/RemoteIntentService: onHandleIntent: command = Button1

1 个答案:

答案 0 :(得分:2)

当您使用PendingIntent.getService(context, 0, intentButton1, 0)(具体而言,0作为最后一个参数)时,不会根据PendingIntent overview替换额外内容:

  

由于这种行为,为了检索PendingIntent,重要的是要知道两个Intent何时被认为是相同的。人们常犯的一个错误是使用Intents创建多个PendingIntent对象,这些对象只在其“额外”内容中有所不同,期望每次都获得不同的PendingIntent。这不会发生。用于匹配的Intent部分与Intent.filterEquals定义的部分相同。如果你使用两个相当于Intent.filterEquals的Intent对象,那么你将获得两个相同的PendingIntent。

他们会详细介绍如何处理它们,但在您的情况下,您可以更改它,以便每个PendingIntent都有唯一的请求代码(第二个参数):

PendingIntent pendingButton1 = PendingIntent.getService(context, 0,
    intentButton1, 0);
PendingIntent pendingButton2 = PendingIntent.getService(context, 1,
    intentButton2, 0);

这将导致Android将它们视为两个单独的PendingIntents。