Implementing a Countdown Timer Using a Service in the Background

What I want to do in my application: 1. When the user opens the application, he will be able to see the already running countdown timer. Therefore, in this case, I want to show the countdown numbers in text form, which will always be updated every second. 2. The user will be able to stop him. 3. The user can leave the application, but the countdown should continue and show the updated time when he returns to the user interface of the application.

So, based on the above points, I understand that I need to implement a Service that runs in the background. I read this link, but my problem is implementation related. I absolutely do not understand how to implement this. However, I also can’t come up with a way to show the time in the user interface as soon as the user returns to the application. I also saw this link , but I'm not sure that only the implementation of CountDownTimer makes it a running service. So, how should I continue and implement it? Any links to specific tutorials / codes are welcome. Thanks.

UPDATES: I could still 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 this: 1. I do not know how to show the countdown from the MainActivity interface other than this Toast. 2. It does not stop counting when I click the Stop button.

+7
source share
1 answer

use bindService (Intent service, ServiceConnection conn, int flag) to link your countdown service, then return a binder that contains your account in your service, and in your activity an instance of the ServiceConnection object can handle the binder. AIDL can also do this, offer a first look at how to connect the service, a desire can help you.

demo

 public class BindService extends Service{ private int count; private boolean quit; private MyBinder binder = new MyBinder(); // My Binder public class MyBinder extends Binder { public int getCount() { // get the counting status:count return count; } } @Override public IBinder onBind(Intent intent) { System.out.println("Service is Binded"); // return the binder instance return binder; } @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created"); // counting work new Thread() { @Override public void run() { while (!quit) { try { Thread.sleep(1000); } catch (InterruptedException e) { } count++; } } }.start(); } // invoke when the service unbind @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbinded"); return true; } @Override public void onDestroy() { super.onDestroy(); this.quit = true; System.out.println("Service is Destroyed"); } @Override public void onRebind(Intent intent) { super.onRebind(intent); this.quit = true; System.out.println("Service is ReBinded"); } 

}

and then activity

 public class MainActivity extends Activity{ Button bind , unbind , getServiceStatus; BindService.MyBinder binder; // define a ServiceConnection object private ServiceConnection conn = new ServiceConnection() { // then the Activity connected with the Service, this will be called @Override public void onServiceConnected(ComponentName name , IBinder service) { System.out.println("--Service Connected--"); // achieve MyBinder instance binder = (BindService.MyBinder) service; } // then the connection break off @Override public void onServiceDisconnected(ComponentName name) { System.out.println("--Service Disconnected--"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bind = (Button) findViewById(R.id.bind); unbind = (Button) findViewById(R.id.unbind); getServiceStatus = (Button) findViewById(R.id.getServiceStatus); final Intent intent = new Intent(); intent.setAction("org.crazyit.service.BIND_SERVICE"); bind.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { //bind Serivce bindService(intent , conn , Service.BIND_AUTO_CREATE); } }); unbind.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { //unbind Serivce unbindService(conn); } }); getServiceStatus.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { // Toast to show the conut value Toast.makeText(MainActivity.this , "Serivce count value is:" + binder.getCount() , 4000) .show(); } }); }} 
+1
source

All Articles