Hello,
I have an application widget which include text and button views. I
create 2 instances of my widget on home screen. I'd like to differ
button clicks depending on particular widget instance it contains and
update appropriate text view only. How could I achieve this behavior?
My steps so far:
1. Created AppWidgetProvider
onUpdate(Context context, AppWidgetManager appWidgetManager, int[]
appWidgetIds) {
for (int widgetId: appWidgetIds)
startUpdateService(widgetId, "initial text");
}
onReceive(Context ctx, Intent intent) {
int widgetId =
intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
if (DO_UPDATE_TEXTVIEW.equals(intent.getAction()))
startUpdateService(widgetId, "refreshed text");
}
startUpdateService(int widgetId, String text) {
Intent intent = new Intent(context, UpdateService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.putExtra(TEXT, text);
context.startService(intent);
}
2. Created IntentService
public void onHandleIntent(Intent intent) {
int widgetId =
intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
String text = intent.getStrintExtra(TEXT);
RemoteViews views = new RemoteViews(this.getPackageName(),
R.layout.widget);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, new
Intent(DO_UPDATE_TEXTVIEW), 0);
views.setOnClickPendingIntent(R.id.button, pIntent);
views.setTextViewText(R.id.text, text);
AppWidgetManager wdgManager = AppWidgetManager.getInstance(this);
wdgManager.updateAppWidget(widgetId, views);
}
The problem is in onReceive() method, actually. widgetId, that I'm
getting from Intent is not correct (always the same value;) not
appropriate widget id, on which my button click really happen. <br>
Would appreciate any help. thanks.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en