Android AppWidget 和 IntentService,两个不同按钮的结果相同
Android AppWidget and IntentService, same result from two different buttons
Android 这里是新手。我有一个包含一个 activity 和一个 AppWidget 的应用程序。它们都向 IntentService 发送命令。 activity 工作正常,但是由两个按钮组成的 AppWidget 无论我单击哪个按钮都会给出相同的结果。
也许我不能像这样从 AppWidget 使用 IntentService ......?我只是在学习。任何帮助将不胜感激。
我的 IntentService class:
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 class:
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
当您使用 PendingIntent.getService(context, 0, intentButton1, 0)
(具体来说,0
作为最后一个参数)时,不会根据 PendingIntent overview:
替换额外内容
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.
他们详细介绍了如何处理它们,尽管在您的情况下您可以更改它,以便每个 PendingIntent
都有一个唯一的请求代码(第二个参数):
PendingIntent pendingButton1 = PendingIntent.getService(context, 0,
intentButton1, 0);
PendingIntent pendingButton2 = PendingIntent.getService(context, 1,
intentButton2, 0);
这将导致 Android 将它们视为两个单独的 PendingIntents。
Android 这里是新手。我有一个包含一个 activity 和一个 AppWidget 的应用程序。它们都向 IntentService 发送命令。 activity 工作正常,但是由两个按钮组成的 AppWidget 无论我单击哪个按钮都会给出相同的结果。 也许我不能像这样从 AppWidget 使用 IntentService ......?我只是在学习。任何帮助将不胜感激。
我的 IntentService class:
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 class:
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
当您使用 PendingIntent.getService(context, 0, intentButton1, 0)
(具体来说,0
作为最后一个参数)时,不会根据 PendingIntent overview:
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.
他们详细介绍了如何处理它们,尽管在您的情况下您可以更改它,以便每个 PendingIntent
都有一个唯一的请求代码(第二个参数):
PendingIntent pendingButton1 = PendingIntent.getService(context, 0,
intentButton1, 0);
PendingIntent pendingButton2 = PendingIntent.getService(context, 1,
intentButton2, 0);
这将导致 Android 将它们视为两个单独的 PendingIntents。