Hi I am using alarm Manager for specific time interval for 3 minutes and I started monitoring. It worked for sometimes and suddenly I noticed there is irregular time interval which is not correct! You can see in attached log where at "20-Jul-2016 12:22:03 pm" time varies! I connected the phone and turned off the screen and monitored! where for every 3 minutes, i hit the server and gets the response as 1. But at one time, it takes 5 minutes to hit the server! Why this strange issue happened?
Here is code.
public void startAt3() {
Intent alarmIntent = new Intent(ActivityTracking.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ActivityTracking.this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Set the alarm */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
/* Repeating on every 3 minute interval */
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
180000L, pendingIntent);
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Log.e("alarm",mydate);
}
AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver {//AlarmReceiver class
@Override
public void onReceive(Context context, Intent intent) {//onReceive method
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Log.e("alarm",mydate);
Intent service = new Intent(context, SimpleWakefulService.class);//intent to call another class
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, service);//service started
}
SimpleWakefulService:
public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");//instantiates simpleWakefulService
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("simpleWakeful","simpleWakeful");
serviceCall(this); //here is downloadTaskMethod called and getting response as 1.
}
What is this permission? The new exact alarm permission ( SCHEDULE_EXACT_ALARM ) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.
Start an alarm when the device restartsBy default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device.
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock.
Don't use setInexactRepeating
. This, as the name suggests, doesn't schedule the alarm to go off at an exact time.
You can use something like this:
public void scheduleSingleAlarm(Context context) {
Intent intent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context,
SINGLE_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar futureDate = Calendar.getInstance();
futureDate.add(Calendar.MINUTE, 3);
setSingleExactAlarm(futureDate.getTime().getTime(), pendingUpdateIntent);
}
@SuppressLint("NewApi")
private void setSingleExactAlarm(long time, PendingIntent pIntent) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
}
}
When you receive a call from the alarm, schedule another alarm to go off in another three minutes. I've blogged about this here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With