Android how do I wait until a service is actually connected?
Asked Answered
A

8

50

I have an Activity calling a Service defined in IDownloaderService.aidl:

public class Downloader extends Activity {
 IDownloaderService downloader = null;
// ...

In Downloader.onCreate(Bundle) I tried to bindService

Intent serviceIntent = new Intent(this, DownloaderService.class);
if (bindService(serviceIntent, sc, BIND_AUTO_CREATE)) {
  // ...

and within the ServiceConnection object sc I did this

public void onServiceConnected(ComponentName name, IBinder service) {
  Log.w("XXX", "onServiceConnected");
  downloader = IDownloaderService.Stub.asInterface(service);
  // ...

By adding all kinds of Log.xx I found that the code after if(bindService(...)) actually goes BEFORE ServiceConnection.onServiceConnected is being called - that is, when downloader is still null - which gets me into trouble. All the samples in ApiDemos avoid this timing problem by only calling services when triggered by user actions. But what should I do to right use this service after bindService succeeds? How can I wait for ServiceConnection.onServiceConnected being called reliably?

Another question related. Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread (mentioned in the doc as the "main thread")? Are there interleaves between them, or Android would schedule all events come into being handled one-by-one? Or, When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

Angora answered 16/6, 2010 at 17:17 Comment(2)
If you still need an answer I gave a solution at https://mcmap.net/q/355848/-can-bindservice-be-made-to-blockVanward
@Alessio, I don't think your solution actually works.Casque
U
55

How can I wait for ServiceConnection.onServiceConnected being called reliably?

You don't. You exit out of onCreate() (or wherever you are binding) and you put you "needs the connection established" code in onServiceConnected().

Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread

Yes.

When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

Your bind request probably is not even going to start until after you leave onCreate(). Hence, onServiceConnected() will called sometime after you leave onCreate().

Urquhart answered 16/6, 2010 at 17:35 Comment(11)
Thanks for this information. Hope the Android document could get things clear like this.Angora
The various Service API demos have examples; see Local Service Binding and Remote Service Binding in particular: developer.android.com/resources/samples/ApiDemos/src/com/… also the Service java doc has the sample code for local service binding: developer.android.com/reference/android/app/…Validity
Thanks hackbod. Apparently this section doesn't appear in my local copy of document. I'll take a look online.Angora
Although I can understand that onCreate() must finish before onServiceConnected() will start (I guess it is pending in the main thread Looper), I notice that onStart() and onResume() ALSO run before onServiceConnected()! Isn't this becoming a dangerous race condition? View components that depend on the Service are ready to go, but the Service isn't connected yet.Cointreau
Related question: are you sure your activity still exists when onServiceConnected is called ?Talyah
@PaulPraet: Good question. I have no idea. The window of potential problems is small, and it's possible that if the activity were destroyed that onServiceConnected() simply would not get called, particularly if you called unbindService() along the way. However, I have not tried tracing through this specific scenario, mostly because I avoid binding like the plague.Urquhart
@Cointreau That is the problem isn't it.. Why doesn't Android just give us a synchronous version of bind?Grin
thanks @Urquhart but the question is when onServiceConnected() will get called e.g. 1 sec, 1 min or never?Starr
@eric You can not count on any particular time. This is why the call is asynchronous.Nonconductor
@Cointreau In onCreate or onStart or onResume, the views can try asking the service object for the values (but first check whether the service object exists), and if the service object is there it should be able to give some value, else revert to some default. Under onServiceConnected, update the views. No matter which happens first, it seems that any state in the service would be synced to the views you are holding in the activity.Kovar
Thanks! I Spent a whole day trying to figure out why my service isn't starting. It was weird because 1 out of 10 times it would start. I assumed it was connected immediately after bindService .Naamann
D
3

I had the same problem. I didn't want to put my bound service dependent code in onServiceConnected, though, because I wanted to bind/unbind with onStart and onStop, but I didn't want the code to run again every time the activity came back to the front. I only wanted it to run when the activity was first created.

I finally got over my onStart() tunnel vision and used a Boolean to indicate whether this was the first onServiceConnected run or not. That way, I can unbindService in onStop and bindService again in onStart without running all the start up stuff each time.

Del answered 9/11, 2012 at 7:50 Comment(0)
G
3

I ended up with something like this:

1) to give the auxiliary stuff some scope, I created an internal class. At least, the ugly internals are separated from the rest of the code. I needed a remote service doing something, therefore the word Something in class name

private RemoteSomethingHelper mRemoteSomethingHelper = new RemoteSomethingHelper();
class RemoteSomethingHelper {
//...
}

2) there are two things necessary to invoke a remote service method: the IBinder and the code to execute. Since we don't know which one becomes known first, we store them:

private ISomethingService mISomethingService;
private Runnable mActionRunnable;

Each time we write to one of these fileds, we invoke _startActionIfPossible():

    private void _startActionIfPossible() {
        if (mActionRunnable != null && mISomethingService != null) {
            mActionRunnable.run();
            mActionRunnable = null;
        }
    }
    private void performAction(Runnable r) {
        mActionRunnable = r;
        _startActionIfPossible();
    }

This, of course, assumes that the Runnable has access to mISomethingService, but this is true for runnables created within the methods of the RemoteSomethingHelper class.

It is really good that the ServiceConnection callbacks are called on the UI thread: if we are going to invoke the service methods from the main thread, we do not need to care about synchronization.

ISomethingService is, of course, defined via AIDL.

3) Instead of just passing arguments to methods, we create a Runnable that will invoke the method with these arguments later, when invocation is possible:

    private boolean mServiceBound;
    void startSomething(final String arg1) {
        // ... starting the service ...
        final String arg2 = ...;
        performAction(new Runnable() {
            @Override
            public void run() {
                try {
                    // arg1 and arg2 must be final!
                    mISomethingService.startSomething(arg1, arg2);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

4) finally, we get:

private RemoteSomethingHelper mRemoteSomethingHelper = new RemoteSomethingHelper();
class RemoteSomethingHelper {
    private ISomethingService mISomethingService;
    private Runnable mActionRunnable;
    private boolean mServiceBound;
    private void _startActionIfPossible() {
        if (mActionRunnable != null && mISomethingService != null) {
            mActionRunnable.run();
            mActionRunnable = null;
        }
    }
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        // the methods on this class are called from the main thread of your process.
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mISomethingService = null;
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mISomethingService = ISomethingService.Stub.asInterface(service);
            _startActionIfPossible();
        }
    }
    private void performAction(Runnable r) {
        mActionRunnable = r;
        _startActionIfPossible();
    }

    public void startSomething(final String arg1) {
        Intent intent = new Intent(context.getApplicationContext(),SomethingService.class);
        if (!mServiceBound) {
            mServiceBound = context.getApplicationContext().bindService(intent, mServiceConnection, 0);
        }
        ComponentName cn = context.getApplicationContext().startService(intent);
        final String arg2 = ...;
        performAction(new Runnable() {
            @Override
            public void run() {
                try {
                    mISomethingService.startSomething(arg1, arg2);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

context is a field in my class; in an Activity, you can define it as Context context=this;

I did not need queuing actions; if you do, you can implement it.

You likely will need a result callback in startSomething(); I did, but this is not shown in this code.

Godard answered 15/8, 2013 at 12:33 Comment(0)
R
3

Android 10 has introduced a new bindService method signature when binding to a service to provide an Executor (which can be created from the Executors).

    /**
     * Same as {@link #bindService(Intent, ServiceConnection, int)} with executor to control
     * ServiceConnection callbacks.
     * @param executor Callbacks on ServiceConnection will be called on executor. Must use same
     *      instance for the same instance of ServiceConnection.
    */
    public boolean bindService(@RequiresPermission @NonNull Intent service,
            @BindServiceFlags int flags, @NonNull @CallbackExecutor Executor executor,
            @NonNull ServiceConnection conn) {
        throw new RuntimeException("Not implemented. Must override in a subclass.");
    }

This allows to bind to the service in a thread and wait until it is connected. E.g. stub:


private final AtomicBoolean connected = new AtomicBoolean()
private final Object lock = new Object();

... 

private void myConnectMethod() {
// bind to service
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    context.bindService(new Intent(context, MyServiceClass.class), Context.BIND_AUTO_CREATE, executorService, new 
   ServiceConnection() {
     @Override
     public void onServiceConnected(ComponentName name, IBinder binder) {
        synchronized (lock) {
            // TODO: store service instance for calls in case of AIDL or local services
            connected.set(true);
            lock.notify();
        }
     });

    synchronized (lock) {
            while (!connected.get()) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException();
                }
            }
        }
}

It is also necessary to run the service in a separate process:

        <service
            android:name=".MyServiceClass"
            android:process=":service"
            android:enabled="true"
            android:exported="true" />
Richrichara answered 23/2, 2021 at 4:3 Comment(1)
I implemented this with a suspendCoroutine and it's amazing how simple it becomes. Thanks! The ExecutorService is the missing piece of the puzzleNephology
S
1

I did something similar before, the only different is I was not binding to service, but just starting it.

I would broadcast an intent from the service to notify the caller/activity about it is started.

Smatter answered 16/6, 2010 at 17:20 Comment(2)
Thanks for the quick reply. So in the broadcast receiver you would bind to the service to call the RPC methods, or are you not calling them in your case?Angora
You can't bind to a service from a broadcast receiver (since the broadcast receive is done once it returns from onReceive).Validity
T
1

I wanted to add some things you should or should not do:

  1. bind the service not on create but onResume and unbind it onPause. Your app can go into pause (background) at any time by user interaction or OS-Screens. Use a distinct try/catch for each and every service unbinding, receiver unregistering etc in onPause so if one is not bound or registered the exception doesn't prevent the others from being destroyed too.

  2. I usually capsule binding in a public MyServiceBinder getService() Method. I also always use a blocking boolean variable so I don't have to keep an eye on all those calls using the servie in the activity.

Example:

boolean isBindingOngoing = false;
MyService.Binder serviceHelp = null;
ServiceConnection myServiceCon = null;

public MyService.Binder getMyService()
{
   if(serviceHelp==null)
   {
       //don't bind multiple times
       //guard against getting null on fist getMyService calls!
       if(isBindingOngoing)return null; 
       isBindingOngoing = true;
       myServiceCon = new ServiceConnection(
           public void onServiceConnected(ComponentName cName, IBinder binder) {
               serviceHelp = (MyService.Binder) binder;
               //or using aidl: serviceHelp = MyService.Stub.AsInterface(binder);
               isServiceBindingOngoing = false;
               continueAfterServiceConnect(); //I use a method like this to continue
           }

           public void onServiceDisconnected(ComponentName className) {
              serviceHelp = null;
           }
       );
       bindService(serviceStartIntent,myServiceCon);
   }
   return serviceHelp;
}
Transmontane answered 24/4, 2020 at 12:48 Comment(0)
A
0

I figured out that these workarounds are only worth the effort and the wait only if your bound services are running in a different process than your application's main process.

For accessing data and methods in the same process (or application), I ended up implementing singleton classes. If the classes need a context for some methods, I leak the application context to the singleton classes. There is, of course, a bad consequence of it as it breaks the "instant run". But that is an overall better compromise, I think.

Allhallowmas answered 14/12, 2016 at 11:33 Comment(0)
M
-1

*The basic idea is same with @18446744073709551615, but I will share my code as well.

As a answer of main question,

But what should I do to right use this service after bindService succeeds?

[Original expectation (but not work)]

wait until service connected like below

    @Override
    protected void onStart() {
        bindService(service, mWebServiceConnection, BIND_AUTO_CREATE);
        synchronized (mLock) { mLock.wait(40000); }

        // rest of the code continues here, which uses service stub interface
        // ...
    }

It won't work because both bindService() in onCreate()/onStart() and onServiceConnected() is called at same main thread. onServiceConnected() is never called before wait finishes.

[Alternative solution]

Instead of "wait", define own Runnable to be called after Service Connected and execute this runnable after service connected.

Implement custom class of ServiceConnection as follows.

public class MyServiceConnection implements ServiceConnection {

    private static final String TAG = MyServiceConnection.class.getSimpleName();

    private Context mContext = null;
    private IMyService mMyService = null;
    private ArrayList<Runnable> runnableArrayList;
    private Boolean isConnected = false;

    public MyServiceConnection(Context context) {
        mContext = context;
        runnableArrayList = new ArrayList<>();
    }

    public IMyService getInterface() {
        return mMyService;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.v(TAG, "Connected Service: " + name);
        mMyService = MyService.Stub.asInterface(service);

        isConnected = true;
        /* Execute runnables after Service connected */
        for (Runnable action : runnableArrayList) {
            action.run();
        }
        runnableArrayList.clear();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        try {
            mMyService = null;
            mContext.unbindService(this);
            isConnected = false;
            Log.v(TAG, "Disconnected Service: " + name);
        } catch(Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    public void executeAfterServiceConnected(Runnable action) {
        Log.v(TAG, "executeAfterServiceConnected");
        if(isConnected) {
            Log.v(TAG, "Service already connected, execute now");
            action.run();
        } else {
            // this action will be executed at the end of onServiceConnected method
            Log.v(TAG, "Service not connected yet, execute later");
            runnableArrayList.add(action);
        }
    }
}

And then use it in the following way (in your Activity class or etc),

private MyServiceConnection myServiceConnection = null;

@Override
protected void onStart() {
    Log.d(TAG, "onStart");
    super.onStart();

    Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
    startService(serviceIntent);
    myServiceConnection = new MyServiceConnection(getApplicationContext());
    bindService(serviceIntent, myServiceConnection, BIND_AUTO_CREATE);

    // Instead of "wait" here, create callback which will be called after service is connected
    myServiceConnection.executeAfterServiceConnected(new Runnable() {
        @Override
        public void run() {
            // Rest of the code comes here.
            // This runnable will be executed after service connected, so we can use service stub interface
            IMyService myService = myServiceConnection.getInterface();
            // ...
        }
    });
}

It worked for me. But there may be more better way.

Mosher answered 20/1, 2016 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.