Search code examples
javaandroidbroadcastreceiver

BroadcastReceiver is not called in Android >= Api 26


BroadcastReceiver ist not working for any action in Api 26 or higher, according to documentation implicit actions are blocked, but not boot_completed. I tested with Android 19, 24, 26 and 28.

Here is my receiver declaration:

<receiver
        android:name="de.com.limto.limto1.broadcastReceiver"
        android:label="StartMyServiceAtBootReceiver"
        android:directBootAware="true"
        android:enabled="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

and here my permissions

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

and here my receiver:

@Override
public void onReceive(Context context, Intent intent) {
    try {
        Log.d(BROADCAST_RECEIVER,intent.getAction());
        lib.appendLog(intent.getAction(),context);
        lib.ShowToast(context, BROADCAST);
        if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
            if (service != null) {
                try {
                    service.stop();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                    Log.e(BROADCAST_RECEIVER,throwable.getMessage(), throwable);
                }
            }
        } else {
            /*if (service != null) return;
            Intent serviceIntent;
            serviceIntent = new Intent(context, LimindoService.class);
            serviceIntent.putExtra(BOOT, true);
            */
            LimindoService.ccontext = context;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //context.startService(serviceIntent);
                lib.scheduleJob(context);
            } else {
                //context.startService(serviceIntent);
                startServiceByAlarm(context);
            }


        }
    }
    catch (Throwable ex)
    {
        ex.printStackTrace();
        lib.appendLog(ex.getMessage(), context);
        Log.e(BROADCAST_RECEIVER,null,ex);
        lib.ShowToast(context, ex.getMessage());
    }
}

Solution

  • This behaviour is happening only on some devices with api >= 26 ... This was caused by the phone settings: Starting the service on boot was disabled in the settings.