1

I have a requirement to logout the user from the app everyday at 3 PM.

I need to call an logout API everyday at 3 PM.

My Android version is 4.4.4.

I have implemented this functionality with alarmManager.It is not triggering at exact time (Sometimes it is triggering with a delay of 1 min n sometimes 15 mins)

Is there a solution to fix this.

Code below...

//Setting the alarm for auto logout

private void CallAutoLogOut() {

boolean alarmUp = (PendingIntent.getBroadcast(this, 0,
new Intent("myactionalarm"),PendingIntent.FLAG_NO_CREATE) != null);

Intent alaramIntent = new Intent("myactionalarm");
alaramIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alaramIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
if (alarmUp)
{
Log.e("alaram", "Alarm is already active");
}else {
Log.e("alaram", "Alarm is set..!"+calendar.getTime());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2*60*1000, pendingIntent);

}

}

// Receiving the broadcast and calling auto logout

@Override
public void onReceive(Context context, Intent intent) {
    this.con = context;
    RequestQueue requestQueue = Volley.newRequestQueue(context);

    Log.e("alaram", "just called reciver");
    if (intent.getAction() != null) {
        if (intent.getAction().equals("myactionalarm")) {
            Log.e("alaram", "onReceive: ----->" + intent);

            callLogoutApi(requestQueue);


        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vijeesh
  • 1,317
  • 1
  • 17
  • 32

1 Answers1

2

You are using the repeating alarm feature of Alarm Manager when you do setInexactRepeating

With this API while your alarm would be repeating, it will never be done on EXACT time and would be set off as per the system resources available. Also this is less memory and energy consuming as EXACT.

If you want to do it at EXACT time and is absolutely necessary, You need to set a single exact alarm, then set a new alarm inside that alarm for the repeat.

To do exact time use setExact()

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Does your server need it to be fired at exactly 3PM? Otherwise you can do a manual strategy to also do automatic logout locally at 3 PM even if your API is not fired.

Also I do believe this strategy would easily fail if someone just switches their Android clock back by sometime so that 3PM doesn't occur and your API never gets fired.

Kapil G
  • 4,081
  • 2
  • 20
  • 32
  • If we use setExact we can fire at exact time but if the user logs again immediately. It will go for a loop and the app will crash. – vijeesh Sep 11 '17 at 10:59
  • How could it go in loop? your exact 3PM will occur only once in a day and lets say if the user logs in exactly at 3 then your service call would depend on whether the session token you now get is old or new? so fire api accordingly – Kapil G Sep 11 '17 at 11:02