0

I am working on the app that runs in background and need to call api for every 1 minute(60*1000 millisecond) without any fluctuations. I have tried Scheduler,timer and things but it is not working proper. For example, my scenario is to call the api on 09:11:36 am(first api call),09:12:36 am(second api call) and so on and at the end the final api call will be at 11:20:36 pm. I used below code :

Handler minuteHandler = new Handler();
minuteHandler.postDelayed(runnable, 60000);
final Runnable runnable = new Runnable() {
@Override
public void run() {
 // your runnable code
 minuteHandler.removeCallbacks(runnable);
 minuteHandler.postDelayed(runnable, 60000);
 }
};

and

new Timer("threadname", true).scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
   // your runnable code              
        }
    }, 0,60*1000);

etc., My question is that it is possible to achieve in android(all OS vesions).

Prasanth
  • 217
  • 1
  • 2
  • 10
  • Where is the code you posted? Activity? Service? Some JobScheduler? – Pawel Jul 27 '18 at 13:29
  • Hi @Pawel, I have tried the code in activity all well as in service. – Prasanth Jul 27 '18 at 13:33
  • 1
    Why do you need such a thing? You probably won't achieve that on any phone since battery will be dead in no time. – egoldx Jul 27 '18 at 13:35
  • Hi @egoldx! Sorry, the things can't be exposed and the battery draining issues are not a problem for me in this scenario. – Prasanth Jul 27 '18 at 13:41
  • You need to use foreground service than. – egoldx Jul 27 '18 at 13:43
  • Ok @egoldx, I will check and let you know – Prasanth Jul 27 '18 at 13:46
  • @Prasanth do you want to make api call even when the application is destroyed? or just as long as the application is in foreground or in the recent task? – MRah Jul 27 '18 at 14:04
  • @MRah, Need to call api in foreground and as well as in background(in recent tasks), the application is not destroyed. – Prasanth Jul 27 '18 at 14:08
  • @Prasanth take a look at this method and answer I have given regarding JobSchedulers https://stackoverflow.com/questions/38344220/job-scheduler-not-running-on-android-n/47760789#47760789, Check out the Doze mode discussion, you will have a good idea. I will post something for you as AlarmManager is best suited for your operation in my opinion, take a look and let me know then I'll create a sample for you – MRah Jul 27 '18 at 14:14
  • @MRah Thanks. I will check this and let you know the feedback – Prasanth Jul 30 '18 at 04:51
  • @MRah I have tried the code as per your reference link, the scheduler works fine but the call back not triggering on particular time interval. – Prasanth Jul 31 '18 at 06:40
  • @Prasanth the job scheduler I posted will not help you in this case, it's because the next time api call happens is INTERVAL+JOB_DONE TIME, you have to restart the job immediately and then make the call. also it's not for doze mode, so for you setAlarmClock() is the best solution – MRah Jul 31 '18 at 15:40
  • @MRah While using setAlarmClock() the alarm not triggers on the exact time. – Prasanth Aug 13 '18 at 05:55
  • @Prasanth, did you log the lag time? Can you post it if it's available, I am going to look into this in more details today and update you – MRah Aug 13 '18 at 15:07

2 Answers2

0

Try AlarmManager.setAlarmClock (API 21) or setExactAndAllowWhileIdle (API 23) with a foreground service. This is the only thing I found working in background in Android's Dose mode.

Please note, Handler stops when your activity is paused or stopped and resumes when activity is resumed.

Not sure about Timer. but, I am sure it won't work in background when device is sleeping.

Jia Li
  • 488
  • 2
  • 8
0

you can achieve that using service till api level 26 but in api level 27 service will automatically kill by the os.

  • 1
    Thanks Siddhesh. I have tried using service and handled the service without killing .My question is that need to call api for every 1 minute(60*1000) without any fluctuations. – Prasanth Jul 27 '18 at 13:56