Android newbie here。私は1つのアクティビティを持つアプリと1つのAppWidgetを持っています。どちらもIntentServiceにコマンドを送信します。アクティビティは正常に動作しますが、2つのボタンで構成されるAppWidgetは、どのボタンをクリックしても同じ結果を出します。 このようなAppWidgetからIntentServiceを使用することはできません...?私はちょうど学んでいます。どんな助けもありがとう。 Android AppWidgetとIntentService、2つの異なるボタンと同じ結果
マイ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上の2つのボタンをクリックし、出力:
D/RemoteIntentService: onHandleIntent: command = Button1
D/RemoteIntentService: onHandleIntent: command = Button1
あなたのレイアウトxmlを投稿してください。 –