Android widget will not have a click

Basically, I have this widget that should show a toast after clicking it.

public class WidgetActivity extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int i = 0; i < appWidgetIds.length; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, WidgetActivity.class); intent.setAction("ActionOne"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); views.setOnClickPendingIntent(R.id.LinLayWiget, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } @Override public void onReceive(Context context, Intent intent) { Log.e("YYY","YYYY"); Toast.makeText(context, "AAA", 1500).show(); if (intent.getAction().equals("ActionOne")) { Log.e("X","X"); Toast.makeText(context, "I'm CLICKED!", 1500).show(); } super.onReceive(context, intent); } } 

manifest:

 ... <receiver android:name="WidgetActivity" android:label="FXMaster" android:icon="@drawable/assiconwi"> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widgetprovider" /> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action> </intent-filter> </receiver> ... 

But after clicking the widget, nothing happens. Any ideas what could be wrong?

Thanks!

+3
source share
1 answer

I'm here to save you.

1) onUpdate override should call (for example, at the beginning of the method)

 super.onUpdate(context, appWidgetManager, appWidgetIds); 

2) Due to this line:

 intent.setAction("ActionOne"); 

Your intentions can be ignored, so wonT code gets in if if onReceive . Add a unique identifier, how is this done in this message Android keeps caching my intentions Extra, how to declare a pending intent that contains fresh additions?

In the last note, I change my class name, since this is actually not an action.

Greetings.

+3
source

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


All Articles