I am working in an application in which I need to refresh my token after every 60 minutes. I have created a service which runs in the background. However, the problem I am facing is, when the phone goes to sleep mode, my service gets stopped and hence I am unable to refresh my token. I am getting this problem mostly in OREO & PIE.
public class InActivityTimer extends Service {
@Override
public void onCreate() {
super.onCreate();
mService = APIUtils_AI.getSOService();
mService2 = APIUtils_AI.getSOServiceAI();
utils = new Utils();
receiver = new BroadcastTokenCheck();
filter = new IntentFilter(COUNTDOWN_BR);
registerReceiver(receiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
cdt = new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
if(millisUntilFinished<8000&&millisUntilFinished>5000){
if(!isSecond){
Log.i("", "Running: ");
refreshToken();//Refresh Token API
cdt.cancel();
cdt.start();
}
}
}
@Override
public void onFinish() {
Intent intent = new Intent(COUNTDOWN_BR);
intent.putExtra("VALUE","startService");
sendBroadcast(intent);
//stopSelf();
Log.i("", "Timer finished");
}
}.start();
return START_STICKY;
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
Log.e("Service Status: ","Stopped");
cdt.cancel();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}