android92 aidl Remote Process Communication

Source: Internet
Author: User

05 Item Remoteservice.java

 PackageCom.itheima.remoteservice;//05 ItemsImportcom.itheima.remoteservice.PublicBusiness.Stub;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder; Public classRemoteserviceextendsService {@Override Publicibinder onbind (Intent Intent) {System.out.println ("Bind Method"); return NewFurong (); }    //before: Class Furong extends Binder implements publicbusiness{    classFurongextendsstub{//directly inherit the stub, because the stub is extends Android.os.Binder implements Com.itheima.remoteservice.PublicBusiness@Override Public voidQianxian () {Banzheng (); }    }         Public voidBanzheng () {System.out.println ("Lee will help you with your license."); }}

05 Item Publicbusiness.aidl

 Package Com.itheima.remoteservice; Interface publicbusiness {    void  Qianxian ();}

05 Project manifest File Configuration service

 <  service  android:name  = "Com.itheima.remoteservice.RemoteService"             >  <  intent-filter                 >  <  action  android:name  = "Com.itheima.remote"             />  </ intent-filter         >  </ service  >  

06 Item Publicbusiness.aidl

 Package Com.itheima.remoteservice; Interface publicbusiness {    void  Qianxian ();}

06 Item Mainactivity.java

 PackageCom.itheima.runremote;//06 ItemsImportcom.itheima.remoteservice.PublicBusiness;Importcom.itheima.remoteservice.PublicBusiness.Stub;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;Importandroid.os.RemoteException;Importandroid.app.Activity;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {PrivateMyserviceconn Conn;    Publicbusiness PB; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Conn=NewMyserviceconn (); }     Public voidClick (View v) {//Start remote ServiceIntent Intent =NewIntent (); Intent.setaction ("Com.itheima.remote");    StartService (Intent); }     Public voidClick2 (View v) {//Stop remote ServiceIntent Intent =NewIntent (); Intent.setaction ("Com.itheima.remote");    StopService (Intent); }         Public voidClick3 (View v) {Intent Intent=NewIntent (); Intent.setaction ("Com.itheima.remote");    Bindservice (Intent, Conn, bind_auto_create); }     Public voidClick4 (View v) {unbindservice (conn); }    classMyserviceconnImplementsserviceconnection{@Override Public voidonserviceconnected (componentname name, IBinder service) {//Turn the IBinder into a publicbusiness .//The returned service is a Furong object inheritance Stub, but the service object is 05 items and is not passed over.PB =stub.asinterface (service); } @Override Public voidonservicedisconnected (componentname name) {}} Public voidClick5 (View v) {Try{Pb.qianxian (); } Catch(RemoteException e) {e.printstacktrace (); }    }}
# # #服务的分类*Local Service: Refers to the activity of the service and the initiating service in the same process*remote service: Refers to the service and the activity that initiates the service is not in the same process (in a different project), the hermit initiates the service. # # #AIDL是AndroidInterfaceDefinition Language Android interface defined language, for interprocess communication technology, the default 2 processes are not able to communicate, Android inside different applications have their own memory space, memory is inaccessible , Linux in the pipeline for process communication , wirelessly also has a plumbing concept, and Handeler is a conduit. 06 Project The way to access the objects of the 05 project is to use process communication, which is equivalent to using process communication to upload 05 of project memory objects to the memory of 06 projects. 1. Extract the remote service method of the 05 project into a separate interface Java file2The suffix of the Java file Publicbusiness.java above the 05 interface is changed to Aidl, which is automatically generated in the 05 project Gen Publicbusiness.java3In the auto-generated Publicbusiness.java file in the 05 project, there is a static abstract stub, which has inherited the binder class, implements the Publicbusiness interface, and this class is the new middleman.4. Copy and paste the Aidl file into the 06 item, when pasting, note that the Aidl file must be in the same package name as the Aidl in the 05 project.5. In the 06 project, when a strong-to-middleman object is used, direct use of stub.asinterface ()

 broadcast receivers are special and can be registered either in the manifest file or directly with the code.  Remote Services can only be started implicitly , like an implicit startup activity, when you configure a service tag in a manifest file, you must configure the intent-Filter child node and specify the action child node. Scenario: A man-in-the-middle object in a remote service, where other applications are not available, cannot be cast when a middleman object is acquired through the bind service, and with Aidl, it is possible to get the interfaces implemented by the intermediate human in other applications .

android92 aidl Remote Process Communication

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.