Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind Service if it's in another process?

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

like image 956
NickUnuchek Avatar asked Feb 28 '17 17:02

NickUnuchek


People also ask

Can a service bind to another service android?

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() .

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.

How do you create a bounded 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.

When binding to a service What does the client do with the IBinder object?

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).


1 Answers

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.

like image 198
CommonsWare Avatar answered Oct 18 '22 17:10

CommonsWare