Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can bindService() be made to block?

Tags:

android

I have an Android application that uses a Remote Service and I bind to it with bindService(), which is asynchronous.

The app is useless until the service is bound, so I would like to simply wait until the binding is finished before any Activity is started. Is there a way to have the service bound before onCreate() or onResume() is called? I think there might be a way to do the binding in Application. Any ideas?

Edit:

if in onCreate() I do this.

bindService(service, mWebServiceConnection, BIND_AUTO_CREATE);
synchronized (mLock) { mLock.wait(40000); }

The ServiceConnection.onServiceConnected doesn't get called for 40 seconds. It's clear that I have to let onCreate() return if I want the service to bind.

So it appears there's no way to do what I want.

Edit 2: Android how do I wait until a service is actually connected? has some good commentary about what is going on in Android when binding a service.

like image 512
vipw Avatar asked Jun 24 '11 18:06

vipw


People also ask

What is bindService ()?

It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

What is the use of onBind () in android?

You can connect multiple clients to a service simultaneously. However, the system caches the IBinder service communication channel. In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds.

Does bind service start the service?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.

When bound service is destroyed?

If you call bindService(), then the service will keep running unless and until you call unbindService() Therefore, if you call both startService() and bindService(), then the service will keep running until you call both stopService and unbindService(). Neither on its own will stop the service.


2 Answers

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.");
    }

See this Answer

like image 66
k_o_ Avatar answered Oct 06 '22 02:10

k_o_


It seems that there is a way to do this. KeyChain.java and several Google-written classes uses a LinkedBlockingQueue to allow synchronously bind to a service.

For example, see the method called bind on this: https://github.com/android/platform_frameworks_base/blob/master/keystore/java/android/security/KeyChain.java

It seems to return the service object synchronously due to the use of blocking queue.

Unfortunately, as stated on the Android docs https://developer.android.com/reference/android/security/KeyChain.html, some methods throws InterruptedException, due to the taking of element from the queue that may be interrupted when waiting.

like image 35
Randy Sugianto 'Yuku' Avatar answered Oct 06 '22 02:10

Randy Sugianto 'Yuku'