## This main activity class ##
> Suggest me with suitable answer
pref = getPreferences(MODE_PRIVATE);
time = pref.getInt("key1", 300);
if (time == 300) {
**getting initial values from shred preferances**
number.setText("01:00:00");
startTimer(time, false);
} else {
startTimer(time, false);
}
}
private void prepareView() {
}
private void setOnListener() {
btnLunchOUT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startTimer(time, true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
btnLunchIN.setEnabled(true);
}
}, 2000);
}
});
Here m storing the value of stopped time in shared preferences
btnLunchIN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
countdownTask.cancel();
btnLunchIN.setEnabled(false);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
btnLunchOUT.setEnabled(true);
}
}, 3000);
btnReset.setEnabled(true);
Toast.makeText(getApplicationContext(), "Timer Stopped",
Toast.LENGTH_SHORT).show();
flag = false;
pref = getPreferences(MODE_PRIVATE);**
SharedPreferences.Editor edit = pref.edit();
edit.putInt("key", time);
edit.commit();
}
});
** Getting Value of starting time from Shared Prefrences**
btnReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!flag) {
// countdownTask.cancel();
btnReset.setEnabled(false);
btnLunchOUT.setEnabled(true);
btnLunchIN.setEnabled(false);
flag = false;
time = 300;
number.setText("00:00:00");// Reset Timing Format
pref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("key", 300);
edit.commit();
}
}
});
}
@Override
public void onResume() { ## OnResume method where i think its problem but i m unable to trace it
System.out.println("In Resume");
super.onResume();
pref = getPreferences(MODE_PRIVATE);
time = pref.getInt("key", time);
number.setText("" + time);
s = (time) % 60;
m = ((time) / 60) % 60;
h = 00;
number.setText("0" + h + ":" + m + ":" + s);
startTimer(time, false);
}
## **onPause method** ##
@Override
public void onPause() {
System.out.println("Timer Pause:");
pref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit(); // Saving the value of timer in shared preferences
edit.putInt("key", time);
edit.commit();
super.onPause();
}
## Here my timer starts on start button click
public void startTimer(int value, boolean isStartButton) {
time = value;
Log.e("ONMAIN:: ", "" + time);
if (isStartButton) {
countdownTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
time--; // ***Here i split 300 into HH:MM:SS format***
s = (time) % 60;
m = ((time) / 60) % 60;
h = 00;
System.out.println("In Run" + time);
> Enabled sound and vibrator on remianing 60 seconds
if (s < 10) {
if (m < 10) {
number.setText("0" + h + ":" + 0 + m + ":"
+ 0 + s);
}
} else if (s < 10) {
number.setText("0" + h + ":" + m + ":" + 0 + s);
} else if (m < 10) {
number.setText("0" + h + ":" + 0 + m + ":" + s);
}
else {
number.setText("0" + h + ":" + m + ":" + s);
}
btnLunchOUT.setEnabled(false);
btnReset.setEnabled(false);
if (time == 60) {
vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrate.vibrate(pattern, -1);
soundalert.start();
System.out.println("1 Minute Remianing ");
}
if (time == 1) {
countdownTask.cancel();
}
}
});
}
};
Timer countdown = new Timer(); //This is timer
countdown.scheduleAtFixedRate(countdownTask, 0, 1000);
flag = false;
}
}
}
The best solution to make a timer run on background is doing it through a Service
.
Create a Service
, which you start (and pass to it the instance of your timer) on the onPause
method and you destroy it(and retrieve the timer) on the onResume
method when you brin your main activity on top.
EDIT:
public class TimerService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("SERVICE", "CREATED");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("SERVICE", "DESTROYED");
//Save your timer instance on destroy service
}
@Override
public void onLowMemory() {
super.onLowMemory();
Log.i("SERVICE", "LOW MEMORY");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Retrieve the timer instance here and use it...
//Restart service if Android destroys it!
return Service.START_NOT_STICKY;
}
}
In your main activity, in onPause
start the service (onPause
is called when activity goes in background):
Intent inetnt=new Intent(this, TimerService.class);
intnet.putExtra("Timer", YourTimerInstance); //Instance as string or int...
startService(inetnt);
In your main activity, in onResume
(called when your activity comes in foreground again) stop the service and retrieve the instance from it. To do this you can use SharedPreferences
(there are thounsands examples of SharedPreferences
in google). To start the service:
Intent inetnt=new Intent(this, TimerService.class);
//Retrieve timer instance
stopService(inetnt);
Hope it helps!