I am working on the code where i need to call some api for every 60*1000 milliseconds both in foreground and background independent of activity/fragment.
I have tried using handler and various other solutions like job scheduler etc.When device is connected to power source or device screen wakes my solution is working fine but when device gets locked it's not working perfectly. Currently i am using the below mentioned logic in application class
Handler minuteHandler = new Handler();
minuteHandler.postDelayed(runnable, 60*1000);
final Runnable runnable = new Runnable() {
@Override
public void run() {
// method to call api
minuteHandler.removeCallbacks(runnable);
minuteHandler.postDelayed(runnable, 60*1000);
}
};
The solution is simple as i need to call api for every 60*1000 milliseconds without lagging in milliseconds for upto 8 to 10 hours continuously until the app gets destroyed.