问题
Hey guys, I have application widget, and I want to send some data to the intent that is attached to PendingIntent, by clicking the widget. here's my code
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, UpComingBDays.class);
if(bdaysAmount != 0){
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("WIDGETIDS", tempAllIDS);
intent.putExtras(bundle);
System.out.println("bund insertedddddddddddddd.....................");
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteView.setTextViewText(R.id.widget_text, finalText4TextView);
remoteView.setOnClickPendingIntent(R.id.WidgetImageButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
I realize always "bund insertedddddddddd......" is printed on CatLog, but the intent's bundle is null.
what is incorrect? how can i send data by clicking on widget. plz dont offer to use services as my code does not have anything with it. many thanks.
回答1:
Try using FLAG_UPDATE_CURRENT
when you create your PendingIntent
.
回答2:
Sorry for answering post after half a year :) For those, who will find this post when get this problem: I have found, that FLAG_UPDATE_CURRENT is not enough:
http://developer.android.com/reference/android/app/PendingIntent.html
This flag will not work if you need more then 1 widget with different data in intents bundles, because Intent will be overrwritten.
To fix this you will need to update intent data:
http://developer.android.com/guide/topics/appwidgets/index.html
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
来源:https://stackoverflow.com/questions/4663242/application-widget-with-bundle