What I want to do in my app: 1. When the user opens the app, he will be able to see an already running countdown timer. So in that case I want to show the countdown numbers in a textview that will always update each second. 2. The user will be able to stop it. 3. The user can leave the application, but the countdown should still go on and show updated time when he comes back to the application UI.
So based on the above points, I understand I need to implement Service that does work in the background. I have read this link but my problem is with the implementation. I am quite at a lost about how to implement that way. That being said, I also cannot think of a way as to showing the time in the UI once the user comes back to the application. I have also seen this link as well, but I am not sure if only implementing CountDownTimer makes it a running Service. So how should I proceed and implement it? Any links to specific tutorials/codes are appreciated. Thanks.
UPDATES: I have so far been able to do the following: MainActivity.java
public class MainActivity extends Activity {
Button btnStart,btnStop;
Intent serviceIntent;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
tv = (TextView) findViewById(R.id.timeView);
//final MyCounter timer = new MyCounter(100000,1000);
//tv.setText("100");
//timer.start();
serviceIntent = new Intent(MainActivity.this,MyService.class);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(serviceIntent);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(serviceIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
MyService.java:
public class MyService extends Service {
MyCounter timer;
@Override
public void onCreate() {
// TODO Auto-generated method stub
timer = new MyCounter(100000,1000);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
timer.start();
return super.onStartCommand(intent, flags, startId);
}
private class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "death", Toast.LENGTH_LONG).show();
stopSelf();
}
@Override
public void onTick(long millisUntilFinished) {
Toast.makeText(getApplicationContext(), (millisUntilFinished/1000)+"", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
timer.cancel();
//stopSelf();
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
The problem is: 1. I don't know how to show the countdown on the MainActivity UI apart from this Toast. 2. It doesn't stop counting when I press Stop Button.