Additional WidgetProvider Intent applications sent with the click of a button are not found in onReceive

I am trying to detect when a widget button is clicked, but none of the extra Intent is displayed in the onReceive method.

onReceive is called with every click, but none of my Intent add-ons appear.

My code is below: I only connect the switch button to the update, so I'm not sure if this is correct. None of the additional functions are displayed, but the categories are null , although I installed this.

onUpdate (context context, etc.):

 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget); Intent buttonIntent = new Intent(context, MyWidgetProviderClass.class); buttonIntent.setAction(ACTION_WIDGET_RECEIVER); buttonIntent.putExtra("BUTTON_CLICKED", "buttonClick"); buttonIntent.putExtra("BUTTON",899); PendingIntent muPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_CANCEL_CURRENT); buttonIntent.addCategory("buttonclick"); remoteViews.setOnClickPendingIntent(R.id.ToggleImageButton, myPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 

OnReceive ():

 intent.getIntExtra("BUTTON",-1); ---> 1 intent.getCategories() --- > null 
+4
source share
3 answers

Try FLAG_UPDATE_CURRENT instead of FLAG_CANCEL_CURRENT .

Also, your code may have a typo: you have muPendingIntent instead of myPendingIntent .

Also, please do not use buttonclick as a category. Please indicate this space (for example, com.something.whatever.buttonclick ) or delete it, as I'm not sure why you need it.

Here is an example project demonstrating an application widget that launches an update on its own with one click, with an additional one (used to deliver application widget identifiers).

+5
source

Android Apparently, I don't like reusing the name ACTION_WIDGET_RECEIVER and removes these options. Created another ACTION only for the toggle button registered in the manifest, and now the parameters are displayed.

+1
source

I found that if the intent that was used to create the pending intent has any additional features that are already present in it, then the new intent is not necessarily ignored. For example, if you follow the pattern in Android docs to create such a widget

 Intent toastIntent = new Intent(context, StackWidgetProvider.class); toastIntent.setAction(StackWidgetProvider.TOAST_ACTION); toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent); 

Then the line

 toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 

Will not impede your new intentions. I deleted this line and my new intention worked.

0
source

Source: https://habr.com/ru/post/1340093/


All Articles