Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Make a BaseActivity for all of your Activities. In the BaseActivity, do the following:</p> <pre><code>public class MyActivity extends Activity implements ServiceConnection { //you may add @override, it's optional protected void onStart() { super.onStart(); Intent intent = new Intent(this, MyService.class); bindService(intent, this, 0); } //you may add @override, it's optional protected void onStop() { super.onStop(); unbindService(this); } public void onServiceConnected(ComponentName name, IBinder binder) {}; public void onServiceDisconnected(ComponentName name) {}; /* lots of other stuff ... */ } </code></pre> <p>Your BaseActivity will need to implement ServiceConnection interface (or you can use an anonymous inner class), but you can leave those methods empty.</p> <p>In your Service class, you need to implement the <code>onBind(Intent)</code> method and return an IBinder. The easiest way to do that is like so:</p> <pre><code>public class MyService extends Service { private final IBinder localBinder = new LocalBinder(); public void onCreate() { super.onCreate(); // first time the service is bound, it will be created // you can start up your timed-operations here } public IBinder onBind(Intent intent) { return localBinder; } public void onUnbind(Intent intent) { // called when the last Activity is unbound from this service // stop your timed operations here } public class LocalBinder extends Binder { MyService getService() { return MyService.this; } } } </code></pre>
 

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