I'm trying to make an app where it requires schedule time to be accurate. I need to reset the log(driver duty info) generated within app at a specific time.
I tried to use setAlarm
but its not working accurately on 6.0 and above. As it should work without internet I can't go with Push Notification.
I also tried to use setAlarmClock
but it doesn't work accurate either.
I need alarm
to be accurate for 1 hour and 24 hour
Update
Log Alarm
public static void startLogAlarm(Context ctx,long milliseconds) {
final int SDK_INT = Build.VERSION.SDK_INT;
// 1 Day = 86400 seconds = 86400000 milliseconds
long timeInMillis = System.currentTimeMillis() + milliseconds;
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctx, eLogReceiver.class);
intent.setAction(Utility.eLOG_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(ctx, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pi);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pi);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
// AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(timeInMillis, pi);
// alarmManager.setAlarmClock(alarmClockInfo, pi);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pi);
}
}
// Start Alarm
Utility.startLogAlarm(this, Constants.DAY_MILLISECONDS);
public class Constants {
public static final long HOUR_MILLISECONDS = 3600000;
public static final long DAY_MILLISECONDS = 86400000;
}
Sensor Alarm
public static void startSensorAlarm(Context ctx, long timeInMillis) {
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.setAction(Utility.SENSOR_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(ctx, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInMillis, pi);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInMillis, pi);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInMillis, pi);
//AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis()+timeInMillis, pi);
alarmManager.setAlarmClock(alarmClockInfo, pi);
}
showLog("Sensor Alarm Time", timeInMillis + "");
}
Group Alarm
public static void startGroupAlarm(Context ctx) {
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.setAction(Utility.GROUP_UPDATE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(ctx, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INTERVAL_EVERY_8_HOURS, pi);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INTERVAL_EVERY_8_HOURS, pi);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INTERVAL_EVERY_8_HOURS, pi);
}
Utility.showLog("Group Alarm", "Started");
}
LogNoticeAlarm
public static void startLogNoticeAlarm(Context ctx, long milliseconds) {
final int SDK_INT = Build.VERSION.SDK_INT;
// 1 Hour = 3600 seconds = 3600000 milliseconds
long HourMilliseconds = System.currentTimeMillis() + milliseconds;
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctx, eLogNoticeReceiver.class);
intent.setAction(Utility.eLOG_NOTICE_ACTION);
PendingIntent pi = PendingIntent.getBroadcast(ctx, 4, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, HourMilliseconds, pi);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, HourMilliseconds, pi);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, pi);
// AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(HourMilliseconds, pi); // alarmManager.setAlarmClock(alarmClockInfo, pi);
}
}
I've been using total 4 alarms out of them 1 required to be schedule at every 5 minutes and rest are 1 hours, 8 hours and 24 hours. so is that sensor alarm( every 5 minutes) is causing the problem for other Alarm?
Please suggest the best solution
Note : All other questions on SO are not working.