ho ottenuto un widget sul mio HomeScreen su cui aggiungo uno scatto a un pulsante. Passo il widget di id dal Widget su di un servizio, ma quando im lettura del WidgetId al servizio è sempre 3.Android: Passando AppWidgetId ad un Servizio di
Widget:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context, WidgetService.class);
// prints the correct appWidgetId
System.out.println(appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
remoteView.setOnClickPendingIntent(R.id.Button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Il servizio è simile al seguente:
public int onStartCommand(Intent intent, int flags, int startId){
if (intent != null && intent.getExtras() != null) {
int widgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
// 3 is always printend
System.out.println(widgetId);
// More stuff here
}
return super.onStartCommand(intent, flags, startId);
}
Quindi cosa sto sbagliando?
grande scoperta. Ho cercato di risolvere questo problema per un po ', e da sempre come nell'esempio di base. Accidenti. Grazie mille! – icecreamman