Android 신참 여기. 하나의 액티비티가있는 앱과 하나의 AppWidget이 있습니다. 둘 다 IntentService에 명령을 보냅니다. 액티비티는 잘 작동하지만 두 버튼으로 구성된 AppWidget은 어떤 버튼을 클릭하든 동일한 결과를 제공합니다. 어쩌면 이처럼 AppWidget에서 IntentService를 사용할 수 없습니까? 나는 단지 배우는 중이다. 어떤 도움을 주시면 감사하겠습니다. Android 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
레이아웃 게시 xml. –