Android 10 has introduced a new [bindService][1] method signature when binding to a service to provide an `Executor` (which can be created from the [Executors][2]).
~~~java
/**
* 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();
}
}
}
}
~~~
[1]: http://aosp.opersys.com/xref/android-10.0.0_r47/xref/frameworks/base/core/java/android/content/Context.java#3007
[2]: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
Android 10 has introduced a new [bindService][1] method signature when binding to a service to provide an `Executor` (which can be created from the [Executors][2]).
~~~java
/**
* 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();
}
}
}
}
~~~
[1]: http://aosp.opersys.com/xref/android-10.0.0_r47/xref/frameworks/base/core/java/android/content/Context.java#3007
[2]: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html