I creating an application using service to running CountDownTimer
in Background.
But the background service sometimes failed or stopped by itself when app closed/killed.
I have tried using START_STICKY
or START_NOT_STICK
but still doesn't work, can't keep the service CountDownTimer
alive. (this the different of them)
I also tried all the suggestion that i found on stackoverflow, but again, doesn't work.
Here my code :
SessionTimerService.java
public class SessionTimerService extends Service{
private CountDownTimer countDownTimer;
public static boolean isServiceRunning = false;
@Override
public void onCreate() {
super.onCreate();
startTimer();
}
@Override
public void onDestroy() {
isServiceRunning = false;
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent!=null && intent.getAction().equals(Constant.ACTION_START_SERVICE)){
startTimer();
}else{
stopTimer();
}
return START_STICKY;
}
private void stopTimer(){
stopForeground(true);
stopSelf();
isServiceRunning = false;
}
private void startTimer(){
if(isServiceRunning)return;
isServiceRunning = true;
String formatCountDown = "%02d:%02d:%02d";
countDownTimer = new CountDownTimer(TimeUnit.MINUTES.toMillis(1), 1000){
@Override
public void onTick(long millisUntilFinished) {
String time = ""+String.format(formatCountDown,
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
Timber.d("The Time : " + time);
Intent broadcast = new Intent(Constant.EVENT_SESSION_TIMER);
broadcast.putExtra(Constant.BROADCAST_TIMER, time);
LocalBroadcastManager.getInstance(App.getInstance().getApplicationContext()).sendBroadcast(broadcast);
}
@Override
public void onFinish() {
Toast.makeText(SessionTimerService.this, "Timer ended!", Toast.LENGTH_SHORT).show();
}
};
countDownTimer.start();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
App.java
I start the CountDownTimer
from the App. here the code :
public class App extends Application {
private ApplicationComponent applicationComponent;
private static boolean isChatActivityOpen = false;
private static boolean isSendChat = false;
private static App instanceApp;
private CountDownTimer countDownTimer;
private Handler handler = new Handler();
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
Timber.plant(new Timber.DebugTree());
instanceApp = this;
// TODO: Add this project to your fabric.io
/*Crashlytics crashlyticsKit = new Crashlytics.Builder()
.core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
.build();
Fabric.with(this, crashlyticsKit);*/
if (applicationComponent == null) {
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(App.get(this)))
.build();
}
}
public static App get(Context context){
return (App) context.getApplicationContext();
}
public ApplicationComponent getComponent(){
return applicationComponent;
}
public static boolean isChatActivityOpen() {
return isChatActivityOpen;
}
public static boolean isSendChat(){
return isSendChat;
}
public static synchronized App getInstance(){
return instanceApp;
}
public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener){
ConnectivityReceiver.connectivityReceiverListener = listener;
}
public static void setChatActivityOpen(boolean isChatActivityOpen) {
App.isChatActivityOpen = isChatActivityOpen;
}
public static void setSendChatStatus(boolean isSendChat){
App.isSendChat = isSendChat;
}
public void startTimer(){
Intent intent = new Intent(getInstance().getApplicationContext(), SessionTimerService.class);
intent.setAction(Constant.ACTION_START_SERVICE);
startService(intent);
}
public void stopTimer(){
Intent intent = new Intent(getInstance().getApplicationContext(), SessionTimerService.class);
intent.setAction(Constant.ACTION_STOP_SERVICE);
stopService(intent);
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
To start the timer in my Activity, i do as follows :
App.startTimer()
or App.stopTimer()
Note : I call App.startTimer()
inside an activity.
Please help to fix this bug. I thank you.