• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to pass extras informations with setOnClickPendingIntent ?

kujaff

Lurker
Hi

I have a widget, with a provider :

PHP:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            Intent intent = new Intent(context, RingModeService.class);
            intent.putExtra("RingModeAppWidgetConfig." + AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
            
            // THIS IS FOR TESTING EXTRAS
            try {
                pendingIntent.send(context, 0, intent);
            } catch (CanceledException e) {
                
            }

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ringmode_widget);
            views.setOnClickPendingIntent(R.id.ringmode_widget_icon, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
}
and the onStart of my service :

PHP:
public void onStart(Intent intent, int startId) {
     intent.getIntExtra("RingModeAppWidgetConfig." + AppWidgetManager.EXTRA_APPWIDGET_ID, 0)
}
Problem :

when i call pendingIntent.send(context, 0, intent); in onUpdate, getIntExtra in the service return the good ID. but when service is called by a click on my widget, extras of the intent are empty, i don't have anything :confused:
 
i'm lucky, after 2h of search, i've just found the problem, i have to add this :

intent.setData(ContentUris.withAppendedId(Uri.EMPTY, appWidgetId));

it's a little bit hard to explain, but setData will made a "really new intent", and it works.
 
Back
Top Bottom