I have a widget running under android, and I would like it to be updated when a user clicks a button on widgets.
For some reason, the onReceive method is never called when a button is pressed after the widget is installed.
I have an onUpdate method similar to this in the AppWidgetProvider class:
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for (int i = 0; i < appWidgetIds.length; ++i) { final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); final Intent nextIntent = new Intent(context, TestAppWidgetProvider.class); nextIntent.setAction(TestAppWidgetProvider.NEXT_ACTION); nextIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setOnClickPendingIntent(R.id.next, refreshPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds[i], rv); } }
And then in my on receive I have a method that looks like this:
@Override public void onReceive(Context ctx, Intent intent) { super.onReceive(ctx, intent); final String action = intent.getAction(); if (action.equals(PREV_ACTION)) { Toast.makeText(ctx, "Previous clicked..", Toast.LENGTH_SHORT).show(); } else if (action.equals(NEXT_ACTION)) { Toast.makeText(ctx, "Next was clicked..", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(ctx, "Other action..", Toast.LENGTH_SHORT).show(); } }
I have in my manifest file:
<receiver android:name="com.test.android.widget.TestAppWidgetProvider" > <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter > <action android:name="com.test.android.widget.PREV" /> </intent-filter> <intent-filter > <action android:name="com.test.android.widget.NEXT" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" /> </receiver>
After installing the widget and clicking on the next button, the onReceive method is never called for some reason ...