Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send EXTRA data via PendingIntent problem

Tags:

android

Hi I'm tried to send extra data via PendingIntent.

This is my code

//**1**
    Intent intent = new Intent(context, UpdateService.class); 
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
    appWidgetId); 
            intent.putExtra(BaseConfigurationActivity.EXTRA_WIDGET_MODE, 
    2); 
            // put appWidgetId here or intent will replace an intent of 
    another widget 
            PendingIntent pendingIntent = 
    PendingIntent.getService(context, appWidgetId, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT); 
            views.setOnClickPendingIntent(R.id.gridview_button, 
    pendingIntent); 

//**2**
            intent = new Intent(context, UpdateService.class); 
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
    appWidgetId); 
            intent.putExtra(BaseConfigurationActivity.EXTRA_WIDGET_MODE, 
    1); 
            // put appWidgetId here or intent will replace an intent of 
    another widget 
            pendingIntent = PendingIntent.getService(context, appWidgetId, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT); 
            views.setOnClickPendingIntent(R.id.listview_button, 
    pendingIntent); 

In my code it assign pendingIntent to two button gridview_button with EXTRA_WIDGET_MODE 2 and listview_button with EXTRA_WIDGET_MODE 1

when I click on gridview_button and it call UpdateService class I also got EXTRA_WIDGET_MODE value is "1"

What i'm doing wrong?

like image 765
SnowBEE Avatar asked Jan 14 '11 07:01

SnowBEE


People also ask

Which methods object creates PendingIntent?

Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

What is PendingIntent callback?

The application specifies a PendingIntent callback (typically an IntentService) which will be called with an intent when activities are detected. The intent recipient can extract the ActivityRecognitionResult using ActivityRecognitionResult.

What is PendingIntent getBroadcast?

getBroadcast(Context context, int requestCode, Intent intent, int flags) Retrieve a PendingIntent that will perform a broadcast, like calling Context. sendBroadcast() .

What is PendingIntent Flag_update_current?

FLAG_UPDATE_CURRENT : Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.


2 Answers

Finally I found problem this happen because I send the same "requestCode"

PendingIntent.getService(context, appWidgetId 

It should be like this

PendingIntent.getService(context, 0 
PendingIntent.getService(context, 1 
like image 63
SnowBEE Avatar answered Nov 15 '22 20:11

SnowBEE


PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Change flag to PendingIntent.FLAG_ONE_SHOT.

like image 45
meizilp Avatar answered Nov 15 '22 20:11

meizilp