How to pass extras informations with setOnClickPendingIntent ?


Last Updated:

  1. kujaff

    kujaff New Member This Topic's Starter

    Joined:
    Jul 18, 2010
    Messages:
    4
    Likes Received:
    0
    Hi

    I have a widget, with a provider :

    PHP:
    1. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    2.         final int N = appWidgetIds.length;
    3.         for (int i=0; i<N; i++) {
    4.             int appWidgetId = appWidgetIds[i];
    5.             Intent intent = new Intent(context, RingModeService.class);
    6.             intent.putExtra("RingModeAppWidgetConfig." + AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    7.             PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    8.            
    9.             // THIS IS FOR TESTING EXTRAS
    10.             try {
    11.                 pendingIntent.send(context, 0, intent);
    12.             } catch (CanceledException e) {
    13.                
    14.             }
    15.  
    16.             RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ringmode_widget);
    17.             views.setOnClickPendingIntent(R.id.ringmode_widget_icon, pendingIntent);
    18.             appWidgetManager.updateAppWidget(appWidgetId, views);
    19.         }
    20. }
    and the onStart of my service :

    PHP:
    1. public void onStart(Intent intent, int startId) {
    2.      intent.getIntExtra("RingModeAppWidgetConfig." + AppWidgetManager.EXTRA_APPWIDGET_ID, 0)
    3. }
    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:
     

    Advertisement
  2. kujaff

    kujaff New Member This Topic's Starter

    Joined:
    Jul 18, 2010
    Messages:
    4
    Likes Received:
    0
    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.
     

Share This Page

Loading...