Search code examples
javaandroidwidgetonclicklistener

onReceive not called for pendingIntent in android home screenwidget


I've been searching for a while on here, but can't find a solution to this. I have a home screen widget with a configuration screen that saves some parameters inside the preferences. The configuration part works flawlessly, but I cannot for the life of me get the setOnClickPendingIntent to register. No error messages.

AndroidManifest.xml

    <receiver android:name=".TrunkWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="ActionTrunkButtonClicked" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/trunk_widget_info" />
    </receiver>

My App widget provider:

public class TrunkWidget extends AppWidgetProvider {
private SharedPreferences pref;
private final String clickAction = "ActionTrunkButtonClicked";

// UPDATE FUNCTION
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);

        // Register an onClickListener
        Intent intent = new Intent(context, TrunkWidget.class);
        intent.setAction(clickAction);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        //Set pendingIntent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}
@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    String action = intent.getAction();
    String actionName = clickAction;

    if (actionName.equals(action)) {
         //DO STUFF
    }
}

I'm pulling my hair on this...if anyone has any idea, I'd be grateful.

Thanks!


Solution

  • Nevermind. I moved this part :

    // Register an onClickListener
        Intent intent = new Intent(context, TrunkWidget.class);
        intent.setAction(clickAction);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    
        //Set pendingIntent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    

    From onUpdate() to updateAppWidget() and now it works. I'm leaving this here in case someone needs it.