Alarms are lost when mobile is turned off and on!

Asked

Viewed 360 times

4

How to make the alarms not get lost?

Ex: if I create one AlarmManager who calls a Broadcast, if I restart(turn off/on) the mobile phone alarm is no longer triggered.

3 answers

3

Make a receiver for the ACTION_BOOT_COMPLETED action, and then you can reconfigure any action you want when the device is turned off/on.

2

Just to complement the answer

Add the following permissions:

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

Configure your receiver:

<receiver android:name=“.MeuReceiver"
    android:enabled="true"
    android:permission="android.permission.ACTION_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

2


As when disconnecting the device alarms are lost you have to create them again at the time the device restarts.

Android launches an Intent(android.intent.action.BOOT_COMPLETED) when the restart is finished.
Take advantage of this and register a Broadcastreceiver to execute code when this situation occurs.

Declare Broadcastreceiver on Androidmanifest.xml

<receiver android:name="aSuaPackage.StartUpBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>  

Add the permission:

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

Write the Broadcastreceiver class:

public class StartUpBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

            // Crie aqui o alarme.
        }
    }
}  

Note:
The way the receiver was declared he is enabled and will be executed whenever the device is restarted.
It can be declared "disabled" using android:enabled="false", as follows:

<receiver android:name="aSuaPackage.StartUpBootReceiver"
          android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>  

You can "activate" it later like this:

ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);

Later you can "disable it" like this:

ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);

For more information see, in the documentation, the guide Scheduling Repeating Alarms.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.