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);
}
}
}