Search code examples
javaandroidwidgetbroadcastreceiver

Widget does not work if app is killed Android 11


I have an application which starts a service when the widget is tapped. This is the class for the widget:

public class PlayWidget extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    
    for (int i = 0; i < appWidgetIds.length; i++) {
        int currentWidgetId = appWidgetIds[i];


        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.playwidget);
        views.setTextViewText(R.id.bLevelText, "My widget");
        
        Intent intent = new Intent(context, PlayService.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pending = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.imageButton11, pending);
        appWidgetManager.updateAppWidget(currentWidgetId, views);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.imageButton11);
    }
}

}

This is the manifest declaration for the Widget:

<receiver
        android:name=".widget.PlayWidget"
        android:icon="@drawable/play">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/playwidget_info" />
    </receiver>

This is the manifest declaration for the Service:

<service android:name=".widget.PlayService"/>

The problem is that on Android 11, when the app is killed and i press the widget, the Service won't start anymore. Anyone has an idea on how can i fix this issue?


Solution

  • I have found an workaround for this issue, by calling a BroadcastReceiver from the OnUpdate function, and then starting the Service from the BroadcastReceiver :

     Intent intent = new Intent();
            intent.setAction(START_SERVICE);
            intent.setClassName(StartServiceReceiver.class.getPackage().getName(), StartServiceReceiver.class.getName());
            PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
            views.setOnClickPendingIntent(R.id.imageButton11, pending);
    

    This is the code for the BroadcastReceiver:

    public class StartServiceReceiver extends BroadcastReceiver {
    
        public static final String START_SERVICE= "start_service";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(START_SERVICE)) {
                context.startService(new Intent(context, PlayService.class));
            }
        }
    }
    

    And the manifest declaration for the BroadcastReceiver:

     <receiver android:name=".StartServiceReceiver" android:label="@string/app_name"/>