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?
According to this:
You'd need to do one of the following: