I have a countdown timer that counts down the current time which is set by the user. Although, I want to have the ability to access other apps or just go to the home screen of the device while having the countdown timer run and continue to update the textview with the timer left in the countdown. Is there any way to do this? I tried implementing a service which is below but I don't know if this is the right way to accomplish this task, for I did receive an error.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_down);
// if (savedInstanceState != null){
//
// }
titleTextView = (TextView)findViewById(R.id.titleTextView);
timeTextView = (TextView)findViewById(R.id.timeTextView);
Bundle getAlarmInfo = getIntent().getExtras();
titleOfAlarm = getAlarmInfo.getString("Title");
totalTime = getAlarmInfo.getString("totalTime");
number = new BigDecimal(totalTime).toBigInteger();
actualTimeFiniliazedInMilliSeconds = number.intValue();
titleTextView.setText(titleOfAlarm);
// countDown = new CountDownTimer(actualTimeFiniliazedInMilliSeconds, timeInterval);
// new UpdateCountDownTime().execute();
countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
countDown.start();
// startService(new Intent(getApplicationContext(), CountDown.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_count_down, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class CountDownTime extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTime(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
timeTextView.setText(hms);
}
@Override
public void onFinish() {
Intent goBack = new Intent(CountDownAct.this, ListOfAlarms.class);
startActivity(goBack);
finish();
}
}
@Override
protected void onPause() {
super.onPause();
timeTextView.setText(hms);
}
// public class CountDown extends Service{
//
// public CountDown () {
//
// }
//
// @Override
// public IBinder onBind(Intent intent) {
// return null;
// }
//
// @Override
// public void onCreate() {
// super.onCreate();
//
// countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
// countDown.start();
// }
// }