Reputation: 3
In my app i need to send notification in scheduled time. It is like medicine reminder app. I found alarmmanager. Read scheduled time list and set to calendar. I found that api 26+ its better using foreground service. I did it. The problem is alarmmanager does not working at exact time. Most of time it delay 2-4 minutes. (in xiaomi phones it much worse :D) So. any solution ? can i check alarmmanager is working(if not restart it) or another way ? My foreground service working well. I check it in screen of,on,user present cases.
for (int i = 0; i < scheduledTimes.length; i++) {
Intent intent2 = new Intent(App.getAppContext(), ForegroundService.class);
AlarmManager alarm = (AlarmManager) App.getAppContext().getSystemService(Context.ALARM_SERVICE);
Date time = new Date((long) scheduledTimes[i][0] * 1000);
calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.SECOND, 0);
if (Build.VERSION.SDK_INT >= 26) {
PendingIntent pintent = PendingIntent.getForegroundService(App.getAppContext(), i, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
} else if (Build.VERSION.SDK_INT >= 23) {
PendingIntent pintent = PendingIntent.getService(App.getAppContext(), i, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
} else {
PendingIntent pintent = PendingIntent.getService(App.getAppContext(), i, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
}
}
Upvotes: 0
Views: 4563
Reputation: 1809
Try this for android 12 by adding this line to Manifest
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Some Additional Notes--
Nowadays devices are coming with more security in context of Battery power consumption. By default devices keep almost all apps in power saving mode. It means in some devices your background work (Location, Alarm manager) won't work as soon as you come out from the app. In other devices background tasks won't work after a battery threshold limit (like 13%). So you need to keep out your app from this battery saving mode to run your app smoothly even in background. The way to achieve that behavior in these two manufacturers is:
Xiaomi Go to the Battery => Power => App battery Saver => select your app and choose No restrictions (for Background settings), then Allow option for Background location.
To AutoStart your app after Boot: Go to the Security app => Permissions => Auto start and check your app.
Samsung Samsung Smart Manager App used to stop all background work after 3 days if you don't come to your app. So the way to disable this feature is:
Go to Battery in the Settings => Unmonitored apps => Add your app to the whitelist. Some other Samsung versions may differ the place to disable it, like Battery => Detail => Select the app and "Don't optimize". For other devices there should be same power options either in settings option directly or some app are given to handle it.
Upvotes: 2