Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Service documentation has fairly complete sample code for implementing a service in your app that another part of your app can bind to and make calls on:</p> <p><a href="http://developer.android.com/reference/android/app/Service.html#LocalServiceSample">http://developer.android.com/reference/android/app/Service.html#LocalServiceSample</a></p> <p>Just put your setUpdateListener() method on the Service, and call it once you get onServiceConnected() with the service.</p> <p>So your code would be something like this:</p> <pre><code>public interface UpdateListener { public void onUpdate(long value); } class LocalService { // Like in the Service sample code, plus: public static String ACTION_START = "com.mypackage.START"; private final ArrayList&lt;UpdateListener&gt; mListeners = new ArrayList&lt;UpdateListener&gt;(); private final Handler mHandler = new Handler(); private long mTick = 0; private final Runnable mTickRunnable = new Runnable() { public void run() { mTick++; sendUpdate(mTick); mHandler.postDelayed(mTickRunnable, 1000); } } public void registerListener(UpdateListener listener) { mListeners.add(listener); } public void unregisterListener(UpdateListener listener) { mListeners.remove(listener); } private void sendUpdate(long value) { for (int i=mListeners.size()-1; i&gt;=0; i--) { mListeners.get(i).onUpdate(value); } } public int onStartCommand(Intent intent, int flags, int startId) { if (ACTION_START.equals(intent.getAction()) { mTick = 0; mHandler.removeCallbacks(mTickRunnable); mHandler.post(mTickRunnable); } return START_STICKY; } public void onDestroy() { mHandler.removeCallbacks(mTickRunnable); } </code></pre> <p>Now you can start the service to get it to start counting, and anyone can bind to it to register a listener to receive callbacks as it counts.</p> <p>It is really hard though to answer your question very well because you aren't really saying what you actually want to accomplish. There are a lot of ways to use services, either starting or binding or mixing the two together, depending on exactly what you want to accomplish.</p> <p>Now you can implement your client code again based on the sample:</p> <pre><code>public class SomeActivity extends Activity implements UpdateListener { private LocalService mBoundService; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mBoundService = ((LocalService.LocalBinder)service).getService(); mBoundService.registerListener(this); } public void onServiceDisconnected(ComponentName className) { mBoundService = null; } }; void doBindService() { bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; } void doUnbindService() { if (mIsBound) { if (mBoundService != null) { mBoundService.unregisterListener(this); } unbindService(mConnection); mIsBound = false; } } protected void onDestroy() { super.onDestroy(); doUnbindService(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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