Does Alarm Manager Persist Even After Reboot

does Alarm Manager persist even after reboot?

A simple answer would be NO. But yes you can achieve this by creating a BroadCastReceiver which will start the Alarm while booting completes of the device.

Use <action android:name="android.intent.action.BOOT_COMPLETED" /> for trapping boot activity in BroadCastReceiver class.

You need to add above line in AndroidManifest.xml as follows,

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

Alarmmanager doesn't work on next day after reboot

AlarmManager doesn't persist even after reboot.
You make AlarmManager work after reboot by creating a BroadCastReceiver which will start the Alarm while booting completes of the device.

Use <action android:name="android.intent.action.BOOT_COMPLETED" /> for trapping boot activity in BroadCastReceiver class.

You need to add above line in AndroidManifest.xml as follows :

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

class StartUpReceiver

public class StartUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
// reset your alarms here
}

}
}

In StartUpReceiver you can create alarm by AlarmManager again. I suggest use need to store alarm info in database or SharedPreferences so will have info to create alarm when device finish reboot.

android alarmmanager alarms after reboot 2016

According to developer.google.com

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Therefore, you will need to create another receiver that would recreate all your alarms. This receiver is not your AlarmReceiver, it does not handle activated alarms. It is used only to reset them after reboot.

To do so you need these code lines:

AndroidManifest.xml

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

<application

<!-- rest of the code -->

<receiver android:name=".AlarmBroadcastReceiver"/>

<service android:name=".BootService"/>

<receiver android:name=".RestartAlarmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

RestartAlarmsReceiver.java

public class RestartAlarmsReceiver extends BroadcastReceiver {

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

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

// It is better to reset alarms using Background IntentService
Intent i = new Intent(context, BootService.class);
ComponentName service = context.startService(i);

if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
else {
Log.e(TAG, "Successfully started service ");
}

} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
}

BootService.java

public class BootService extends IntentService {

public BootService() {
super("BootService");
}

@Override
protected void onHandleIntent(Intent intent) {

// Your code to reset alarms.
// All of these will be done in Background and will not affect
// on phone's performance

}
}

android - What do I need to keep to persist alarm after reboot?

PendingIntents will not persist after reboot, so to be safe just restart your alarms in the BroadcastReceiver with all the intent extras that you make when you first initialized the alarm, and keep the requestcode the same.

How to keep Alarm Manager running even after the app closes?

Use of AlarmManager is very unreliable, especially from Android M onward. Alarm events are ignored by phone doze mode. I use Firebase JobDispatcher https://github.com/firebase/firebase-jobdispatcher-android that serves great for this purposes. Code:
Gradle:

implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

Job class:

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyTestService extends JobService {
@Override
public boolean onStartJob(JobParameters job) {
// something like
Log.d("tag_tag_tag", "Service running");

String filename = "example.txt";
String fileContents = "\ndone ";
FileOutputStream fos = null;

try {
fos = openFileOutput(filename, MODE_APPEND);
fos.write(fileContents.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return false; // Answers the question: "Is there still work going on?"
}

@Override
public boolean onStopJob(JobParameters job) {
return false; // Answers the question: "Should this job be retried?"
}

}

Manifest:

<service
android:exported="false"
android:name=".MyTestService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>

In your activity, create dispatcher:

// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

then schedule the job:

Job myJob = dispatcher.newJobBuilder()
.setService(MyTestService.class) // the JobService that will be called
.setTag("my-unique-tag") // uniquely identifies the job
.setRecurring(true)
.setTrigger(Trigger.executionWindow(1*60-10,1*60+10))//1 minute +/- 10 seconds
.build();

dispatcher.mustSchedule(myJob);

This should work, at least it works for me.

Scheduler with Alarm Manager... doesnt work after reboot

For this we need a Broadcast receiver that should receive "BOOT_COMPLETED" broadcast. We must know that when a device finishes booting Android System sends "BOOT_COMPLTED" broadcast.

Registering the BootReciever in android manifest file

               <receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

and do not forget to add the following permission in manifest .

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

this permission is required to listen/reverie the BOOT_COMPLETED action

BootReceiver.java

   public class BootReceiver extends BroadcastReceiver
{

public void onReceive(Context context, Intent intent)
{

// Your code to execute when Boot Completd
**Schedule your Alarm Here**
Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();

}

}

The onRecieve() method of BootReceiver will execute when boot completes, so wee need to write the code inside onreceive() method



Related Topics



Leave a reply



Submit