Android newbie ici. J'ai une application avec une activité, et un AppWidget. Ils envoient tous les deux des commandes à un IntentService. L'activité fonctionne très bien, mais AppWidget, qui se compose de deux boutons, donne le même résultat quel que soit le bouton sur lequel je clique. Peut-être que je ne peux pas utiliser IntentService depuis un AppWidget comme ça ...? Je suis en train d'apprendre. Toute aide serait appréciée.Android AppWidget et IntentService, même résultat de deux boutons différents
Ma classe 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);
}
}
Ma classe 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);
}
}
}
La sortie lorsque je clique sur les deux boutons sur le AppWidget:
D/RemoteIntentService: onHandleIntent: command = Button1
D/RemoteIntentService: onHandleIntent: command = Button1
Postez votre mise en page xml. –