Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Android, services are started in one of two ways - through the <code>startService(Intent i)</code> method, or the <code>bindService(Intent i)</code>. The method used to start the service determines whether it is started or bound. A service can be started, then bound to a client - or bound and then have calls to start sent to it (it doesn't restart if already started).</p> <p>As you mention listening for <code>BOOT_COMPLETE</code>, I assume this is an action for an <code>Intent</code>, which is sent via a <code>Broadcast</code> object. This means that you can create an <code>IntentFilter</code> object with the <code>BOOT_COMPLETE</code> action added to it via the <code>addAction(String action)</code> method. Then a <code>BroadcastReceiver</code> object can be created, which upon receiving an intent with an action of <code>BOOT_COMPLETE</code> can then call the <code>startService(Intent i)</code> method (this is done by overriding the <code>onReceive(Context context, Intent intent)</code> method).</p> <p>If you call <code>startService(Intent i)</code> when the <code>Intent</code> is received, then the service will be a started service. This means that it will only stop when a call to <code>stopService(Intent i)</code> is made by the app, or if the service calls the <code>stopSelf()</code> method. It can be bound and unbound from by multiple activities during the time it is running, and it will not stop (as it is started, not bound).</p> <p>Here is an example of this, using two <code>Activity</code> objects and a <code>Service</code>:</p> <p><strong>Activity 1 (first activity of your app):</strong></p> <pre><code> public class ServiceActivity extends Activity { private IntentFilter filter = new IntentFilter(); private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if(action.equals(BOOT_COMPLETE) { startService(new Intent(ServiceActivity.this, MyService.class)); } } }; @Override protected void onStart() { super.onStart(); filter.addAction(BOOT_COMPLETE); registerReceiver(receiver, filter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(receiver); } //Some other code } </code></pre> <p><strong>Activity 2 (used at some point after activity 1):</strong></p> <pre><code> public class AnotherActivity extends Activity { private MyService service; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { service = ((MyService.MyBinder)service).getService(); } @Override public void onServiceDisconnected(ComponentName name) { service = null; } }; @Override protected void onStart() { super.onStart(); bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); unbindService(connection); } //Some other code } </code></pre> <p><strong>Service:</strong></p> <pre><code> public class MyService extends Service { private MyBinder binder = new MyBinder(); @Override public IBinder onBind(Intent intent) { return binder; } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return START_STICKY; } //Some other code final class MyBinder extends Binder { MyService getService() { return MyService.this; } } </code></pre> <p><strong>Final notes</strong></p> <p>To be able to use the service as bound, you need to override the <code>onBind(Intent intent)</code> method, and return an instance of binder <code>MyBinder</code>. Not doing so will result in not being able to bind (the binding sets the service variable by using the <code>getService()</code> method defined in <code>MyBinder</code>).</p> <p>The <code>BroadcastReceiver</code> must alwasy be unregistered when the <code>Activity</code> it's in closes, as it would be leaked otherwise. That is why in the example, I have registered and unregistered in the <code>onStart()</code> and <code>onStop()</code> methods respectively. Using <code>onDestroy()</code> to unregister is not recommended as it is not always called.</p> <p>The <code>MyService</code> object that is used when binding must also be unbound when it's <code>Activity</code> closes, as it too can be leaked. It is set to null when <code>onServiceDisconnected(ComponentName name)</code> is called for garbage collecting.</p> <p><strong>Sources for further reading</strong></p> <ul> <li><a href="https://developer.android.com/reference/android/content/BroadcastReceiver.html" rel="nofollow">https://developer.android.com/reference/android/content/BroadcastReceiver.html</a></li> <li><a href="https://developer.android.com/reference/android/app/Activity.html" rel="nofollow">https://developer.android.com/reference/android/app/Activity.html</a></li> <li><a href="https://developer.android.com/reference/android/app/Service.html" rel="nofollow">https://developer.android.com/reference/android/app/Service.html</a></li> <li><a href="https://developer.android.com/reference/android/content/ServiceConnection.html" rel="nofollow">https://developer.android.com/reference/android/content/ServiceConnection.html</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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