Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>use bindService(Intent service, ServiceConnection conn, int flag) to bind your countdown service, then in your service, return your binder that contain your count, and in your activity, a instantiated ServiceConnection object can handle the binder. AIDL can do this as well, suggest have a look at how to bind service firstly, wish can help you.</p> <p>demo</p> <pre><code>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"); } </code></pre> <p>}</p> <p>and then the activity</p> <pre><code>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's count value is:" + binder.getCount() , 4000) .show(); } }); }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload