and and and

Android - Broadcast not working after reboot

102 Views Asked by At

In AndroidManifest.xml:

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

and

<receiver android:name=".alarm.AlarmBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

AlarmBroadcastReceiver.java:

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Broadcast", Toast.LENGTH_LONG).show();
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Toast.makeText(context, "Boot...", Toast.LENGTH_LONG).show();
        }
    }

}

In build.gradle:

android {
compileSdkVersion 30
buildToolsVersion '28.0.3'

defaultConfig {
    minSdkVersion 23
    targetSdkVersion 30
    multiDexEnabled true

The app works fine on the simulator, but on the device the broadcast does not start after restart.

Tested simulators: Pixel with API 29, 30 and 31

Tested devices: Honor with Android 9, Samsung with Android 10.

Any idea?

1

There are 1 best solutions below

0
frezq On

According to this:

Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

You'd need to do one of the following:

  • Create the receiver at runtime by calling Context.registerReceiver(), instead of declaring the receiver in the manifest.
  • Use a scheduled job to check for the condition that would have triggered the implicit broadcast.