Manifest:
<service android:name="com.example.MainService" android:process=":main_service"/>
Trying to bind service in Activity:
public class MainActivity extends Activity {
MainService mMainService;
private boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
bindService(intentForMainService, mConnection, Context.BIND_AUTO_CREATE)
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
MainService.MainServiceBinder binder = (MainService.MainServiceBinder) service;//HERE IS EXCEPTION
mMainService = (MainService) binder.getService();
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
mMainService = null;
mBound = false;
}
};
@Override
protected void onStop() {
doUnbindService();
super.onStop();
}
void doUnbindService() {
if (mBound) {
unbindService(mConnection);
}
}
}
Error:
FATAL EXCEPTION: main
Process: com.hos.android, PID: 9001
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.example.service.main.MainService$MainServiceBinder
at com.example.ui.base.BaseServiceActivity$1.onServiceConnected(MainActivity.java:34)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1335)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1352)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
But when I delete this android:process=":main_service" all works properly
Binding to a started service As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService() , which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService() .
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.
There are three ways of creating bound services: First is by extending the binder class. Second is by using a messenger. Third, is by using AIDL or Android Interface Definition Language.
After the client receives the IBinder, it can begin interacting with the service through that interface. onBind(): The system invokes this method by calling bindService() when another component wants to bind with the service (such as to perform RPC).
Step #1: Write an AIDL file that describes the interface to be exported by the service that clients can bind to. For the purposes of this answer, I will call this interface Foo
, and so the AIDL file would be Foo.aidl
. Note that if the client and service are in separate Android Studio modules that both need the same Foo.aidl
content.
Step #2: Have your service's binder extend Foo.Stub
and override the methods on Foo.Stub
, instead of extending IBinder
.
Step #3: In your client, in onServiceConnected()
, convert the raw binder to a Foo
instance via Foo.Stub.asInterface(service)
, and Foo
has the client side of the AIDL-defined API.
This pair of sample projects illustrates this, where in my case the client and the service are in separate apps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With