Android Question Now - Android 6 ServiceStartAtExact not accurate

wes58

Active Member
Licensed User
With KitKat, Google changed AlarmManager such that if you wanted the Service to start at exactly set time you had to use AlarmManager new method setExact (int type, long triggerAtMillis, PendingIntent operation).
Erel implemented this in B4A as StartServiceAtExact.
Now I have upgraded to android 6.0.1 and (not) to my surprise this function is no longer accurate. I get sometimes couple of minutes (6, 7 min.) difference from the scheduled time.
This of course is the result of Google introducing a "Doze" mode.

I tried to use AlarmManager method setExactAndAllowWhileIdle (not tested yet), but searching the web I found that this doesn't fix the problems.
On stackoverflow http://stackoverflow.com/questions/33432661/alarm-manager-for-background-services I found a Java example that uses WakefulBroadcastReceiver that supposed to work in Doze mode. But I am not sure yet how to implement it. Maybe someone with more Java knowledge (or Erel) could have a look at this - i.e. how to implement WakefulBroadcastReceiver. There is also a link there to the CommonsWare website with an example on Github https://github.com/commonsguy/cwac-wakeful

The code is:
B4X:
public class PollReceiver extends WakefulBroadcastReceiver{
    static final String PERIOD = "period";
    @Override
    public void onReceive(Context context, Intent intent){
         startWakefulService(context,new Intent(context,MyService.class));
         long period = intent.getLongExtra(PERIOD,-1);
         if(period>0){
         scheduleExactAlarm(context,(AlarmManager)context.getSystemService(Context.ALARM_SERVICE),period)
         }
    }

    static void scheduleExactAlarm(Context context,AlarmManager alarms, long period){
    Intent i = new Intent(context,PollReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context,0,i,0);
    if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1){
        alarms.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + period,pi);
    }
}
 

wes58

Active Member
Licensed User
But I'm using Erel's code and it doesn't work. He seemed to imply it would work without needing a new jar file.

Which jar should I use?
Erel wrote that you have to use newer android.jar, and as Don wrote setExactAndAllowWhileIdle is introduced in Api 23. So you have to use android.jar from directory /android-sdk/platforms/android-23/.
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

NeoTechni

Well-Known Member
Licensed User
I did. Thank you. It's just a pain having to keep fixing stuff that Google breaks.

Doze mode broke my alarm clock (making me late for work the day they did it). The first fix I tried got my app banned for a few days.
M broke all my file downloading code.
First they removed the search hardware button, then the menu button. Then they removed the menu soft button.
Then they made it so only one app could access the SMS system
Then they made it so no third party app could answer calls (this really screwed me over)
It's ridiculous

Also, you can't have both those OK libraries at the same time. B4A complains that it's defined twice.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
My project is too big to upload.

However today the alarm went off 5 minutes late. In a discussion on G+ I was told of API specifically for alarm clocks that even shows an alarm clock in the menu bar. Anyone know how to use that? A late alarm clock is useless...
 
Upvote 0

lazreg

Active Member
Licensed User
i have the same problem , my alarm often couldn't fire at exact time and when the screen is switched on my app and service sometimes work fine but
when i switched off my screen i noticed the service doesn't work or sometimes works every 5 minutes and i didn't schedule the service for every 5 minutes
, i schedule the service every minute and compare it with saved alarm time, i didn't understand what happens ?
i can't publish my app for a long time about 3 years because i couldn't solve this issue
please who can help me? i will send him the whole project..
thanks
i tested it on SDk 10,22,23
 
Upvote 0

lazreg

Active Member
Licensed User
thanks Erel,,, but i check my alarm times every minute in the service and compare it with saved alarm time,, then could you guide me for the exact and accurate way to fire the service? i have 6 alarms every day all the year and i retrieve the alarm times from database .
 
Upvote 0

lazreg

Active Member
Licensed User
i updated some codes but still not working... who can help me i will make a donation for him ..

thank you in advance.
 

Attachments

  • testAarm.zip
    338.9 KB · Views: 384
Last edited:
Upvote 0

wes58

Active Member
Licensed User
I started this thread in march last year and after using StartServiceAtExactWhileIdle I don't have any problems. I have been using alarm/profile change 3 times a day, every day and it works fine. I have used Android 6 on Galaxy S6edge, then I changed to Galaxy S7 and Android 7 and it works fine. I will try the code in post #31 when I have some time and see if it works for me.
 
Upvote 0

Sorin Pohontu

Member
Licensed User
Hi all,
I've been struggling for 3 days with a similar issue on Samsung phones (Android 6.0 and up): one service was re-started correctly and another one was re-stared at an random interval

The issue appear to be with Samsung implementation of AlarmManager. There are two solutions:
- Service should restart under 15 seconds interval (14 seconds or less)
- Add keyword "alarm" or "alert" in the app package name

See details here: https://stackoverflow.com/questions...arm-is-always-3-minutes-off/34085645#34085645

Have a nice day,
/Sorin
 
Upvote 0
Top