'Start activity or service on boot Android 11 not work

I need to start a push notification service when the phone boots.

I have followed various guides and various answers also to questions asked on this site but without success. The app works (no errors in the log) but at boot it does not start the service.

This is my manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

This is my class

public class AutoStarter extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
             Log.d("test", "in    boot ok ");
            Intent ii = new Intent(context.getApplicationContext(), MainActivity.class);
            ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | 
            Intent.FLAG_ACTIVITY_NEW_TASK);
            ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(ii);
            Log.d("test", "   after boot ok ");
         }

In log, after reboot, i see ACTION_BOOT_COMPLETED is OK.



Solution 1:[1]

You need to provide the auto start permission

You need to provide the auto start permission to the app or disable the restricted app launch https://stackoverflow.com/a/45953515/6209105

Make sure the receiver is defined in the manifest file.

<receiver
        android:name=".BootReciever"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shaikh Mohib