Other process calls Commomservice

Source: Internet
Author: User
Tags stub mremote

Implementing the Service method

 Public class extends Stub {@Override    publicvoidthrows  remoteexception {        this . name = name;        LOG.I ("MAGC", "setname--" +name);    }

Set up a proxy service

Class MyService extends Service {
Private New Myplayer (); @Override public ibinder onbind (Intent arg0) { return player; }

into the public service

  Class sm;         Try {            = Class.forName ("Android.os.ServiceManager");             New Class[] {String.  Class  });             New object[]{"Lovefang.stadyService.IPlayer", player});

Other process calls:

 Private StaticIPlayer Bridge; Static{Class sm; Try{SM= Class.forName ("Android.os.ServiceManager"); Method GetService= Sm.getmethod ("GetService",NewClass[] {String.class }); IBinder Binder= (IBinder) getservice.invoke (SM,NewObject[] {"Lovefang.stadyService.IPlayer" }); Bridge=IPlayer.Stub.asInterface (binder); } Catch(Exception e) {e.printstacktrace (); }  }   Public Static voidTodo ()throwsremoteexception{Bridge.setname ("Test"); }

With:

 Public InterfaceIPlayerextendsAndroid.os.IInterface {/**Local-side IPC implementation stub class.*/     Public Static Abstract classStubextendsAndroid.os.BinderImplementsIPlayer {Private Static FinalJava.lang.String descriptor = "Lovefang.stadyService.IPlayer"; /**Construct The stub at attach it to the interface.*/         PublicStub () { This. Attachinterface ( This, descriptor); }        /*** Cast an IBinder object to an Com.magc.rpc.IPlayer interface, * Generating a proxy if needed. */         Public StaticIPlayer asinterface (Android.os.IBinder obj) {if(obj = =NULL)) {                return NULL; } android.os.IInterface iin=(android.os.IInterface) obj. Querylocalinterface (descriptor); if(((Iin! =NULL) && (iininstanceofIPlayer)))  {                return((IPlayer) iin); }            return NewIPlayer.Stub.Proxy (obj); }         PublicAndroid.os.IBinder Asbinder () {return  This; } @Override Public BooleanOntransact (intcode, Android.os.Parcel data, android.os.Parcel reply,intflags)throwsandroid.os.RemoteException {Switch(code) { Caseinterface_transaction: {reply.writestring (descriptor); return true; }             Casetransaction_setname: {data.enforceinterface (descriptor);                Java.lang.String _arg0; _arg0=data.readstring ();  This. SetName (_ARG0);                Reply.writenoexception (); return true; }             Casetransaction_addfile: {data.enforceinterface (descriptor);                Java.lang.String _arg0; _arg0=data.readstring ();  This. AddFile (_ARG0);                Reply.writenoexception (); return true; }             Casetransaction_tostring: {data.enforceinterface (descriptor); Java.lang.String _result= This.                ToString ();                Reply.writenoexception ();                Reply.writestring (_result); return true; }            }            return Super. Ontransact (Code, data, reply, flags); }        Private Static classProxyImplementsIPlayer {PrivateAndroid.os.IBinder Mremote; Proxy (Android.os.IBinder remote) {mremote=remote; }             PublicAndroid.os.IBinder Asbinder () {returnMremote; }             Publicjava.lang.String Getinterfacedescriptor () {returndescriptor; }             Public voidsetName (java.lang.String name)throwsandroid.os.RemoteException {android.os.Parcel _data=Android.os.Parcel.obtain (); Android.os.Parcel _reply=Android.os.Parcel.obtain (); Try{_data.writeinterfacetoken (descriptor);                    _data.writestring (name); Mremote.transact (Stub.transaction_setname, _data, _reply,0);                _reply.readexception (); } finally{_reply.recycle ();                _data.recycle (); }            }             Public voidAddFile (java.lang.String f_name)throwsandroid.os.RemoteException {android.os.Parcel _data=Android.os.Parcel.obtain (); Android.os.Parcel _reply=Android.os.Parcel.obtain (); Try{_data.writeinterfacetoken (descriptor);                    _data.writestring (F_name); Mremote.transact (Stub.transaction_addfile, _data, _reply,0);                _reply.readexception (); } finally{_reply.recycle ();                _data.recycle (); }            }             Publicjava.lang.String ToString ()throwsandroid.os.RemoteException {android.os.Parcel _data=Android.os.Parcel.obtain (); Android.os.Parcel _reply=Android.os.Parcel.obtain ();                Java.lang.String _result; Try{_data.writeinterfacetoken (descriptor); Mremote.transact (stub.transaction_tostring, _data, _reply,0);                    _reply.readexception (); _result=_reply.readstring (); } finally{_reply.recycle ();                _data.recycle (); }                return_result; }        }        Static Final intTransaction_setname = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 0); Static Final intTransaction_addfile = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 1); Static Final intTransaction_tostring = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 2); }     Public voidsetName (java.lang.String name)throwsandroid.os.RemoteException;  Public voidAddFile (java.lang.String f_name)throwsandroid.os.RemoteException;  PublicJava.lang.String ToString ()throwsandroid.os.RemoteException;}

Other processes call Commomservice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.