How can a remote Service send messages to a bound Activity?

后端 未结 4 1968
我寻月下人不归
我寻月下人不归 2021-02-08 19:44

I\'ve read the documentation about Bound Services, where it is shown that you can easily communicate through Messages from an Activity to a remote (i.e. not in the same

4条回答
  •  旧时难觅i
    2021-02-08 20:01

    1) implement transact/onTransact methods in own Binder.class and binder proxy implementing IInterface.class objects (anon or by extending a class direct) by use of passed in those methods Parcel.class object
    2) attach local interface to own Binder object 3) create service and return a binder proxy implementation from onBind method 4) create bond with bindService(ServiceConnection) 5) this will result in returning proxy binder via created bound in interfece implementation

    this is an android implementation of IPC with usage of kernel binder space

    simplifying in code example :

    class ServiceIPC extends Service {
    
        @Override
        public Binder onBind()  {
            return new IInterface() {
    
                IInterface _local = this;         
    
                @Override 
                public IBinder asBinder() {
                   return new Binder() 
    
                               {   
                                   //
                                   // allow distinguish local/remote impl 
                                   // avoid overhead by ipc call 
                                   // see Binder.queryLocalInterface("descriptor");
                                   //
                                   attachLocalInterface(_local,"descriptor");
                               }
    
                               @Override
                               public boolean onTransact(int code,
                                                         Parcel in,
                                                         Parcel out,
                                                         int flags) 
                                       throws RemoteException {
                                   //
                                   //  your talk between client & service goes here 
                                   //
                                   return whatsoever // se super.onTransact(); 
                               }
                          }
                }    
    
            }.asBinder();
        }
    
    }
    

    *then you could use the IBinder on client and service side the transact method to talk with each other (4 example using odd/eve codes to disgusting local remote side as we use same onTransact method for booth sides)

提交回复
热议问题