Aidl and Service

Source: Internet
Author: User

Service:local service, a process of multithreaded services.

Aidl:remote service, communication between different processes.

Service Startup method:

StartService (): The caller destroy then the service will not be destroyed, not directly applicable to the caller, and will not return any results.

Bindservice (): The caller destroy and the service is destroyed, and can communicate directly with the caller. Returns binder.

* Bindservice can communicate directly with the caller in the form of binder transfer. The caller needs to override the class called Serviceconnnection to communicate directly with the service.

After the binding succeeds, the service object is obtained through GetService () and the service object is set to NULL, indicating that the binding is unexpectedly invalidated and the service instance is no longer available.

Please refer to log:

The procedure first starts the service with StartService and then destroy.

Then Bindservice binds the service and then destroy the result as follows:

It is not difficult to see that the start mode starts the service when the caller destroys the service without automatically stop.

The Bind method binds the service when the caller destroys the service automatically while the binding is destroyed.

Life cycle Related:

The difference between Aidl and service

You can try to create two packages without aidl the case.

The Threadrandomservicedemo class in the Threadrandomservicedemo package to open the Randomservice in the demo can be started but not available,

Some would say that using Bindservice () is not the use of Serviceconnection classes to return binder objects? Yes, but you can't load the demo package (Imorpt Com.example.demo.RandomService) so we can't communicate with service direct data.

But we can use AIDL to call other processes (or other project) to communicate directly.

Aidl is obtained by MyAidl.Stub.asInterface (binder);

Aidl is an interface for obtaining other processes to implement IPC communication.

You can refer to the previously mentioned http://www.cnblogs.com/hongguang-kim/p/5165523.html using AIDL related

A brief description of the Aidl ask price automatically generated in the Java file we can see parcel.

Parcel is a container for data transfer between applications in an Android system, capable of packaging and unpacking data in two processes.

But parcel is different from general-purpose serialization, and parcel is designed for high-performance IPC transmissions, so it is not possible to store parcel objects on any persistent storage device.

When the data is passed to the inside of a cross-process service as a parcel object, the Ontransact () method reads each parameter one at a time from the parcel object and then invokes the method that is developed internally by the service.

And then writes the result to another parcel object, ready to return the parcel object to the remote caller.

Custom Parcel:

First build the Allresult.aidl file, declare the Allresult class

Package edu.hrbeu.parcelmathservicedemo;parcelable Allresult;

Use Parcelable to declare custom classes in line 2nd, so that other aidl files can use this custom class

Constructs the Allresult class. In addition to the basic constructors, the Allresult class requires a constructor that takes the parcel object as input and requires the overloaded wrapper function Writetoparcel ()
The complete code for the Allresult.java file is as follows

 PackageEdu.hrbeu.ParcelMathServiceDemo;ImportAndroid.os.Parcel;Importandroid.os.Parcelable; Public classAllresultImplementsparcelable { Public LongAddresult;  Public LongSubresult;  Public LongMulresult;  Public DoubleDivresult;.  PublicAllresult (LongAddrusult,LongSubresult,LongMulresult,DoubleDivresult) {Addresult=Addrusult; Subresult=Subresult; Mulresult=Mulresult; Divresult=Divresult; }           PublicAllresult (Parcel Parcel) {Addresult=Parcel.readlong (); Subresult=Parcel.readlong (); Mulresult=Parcel.readlong (); Divresult=parcel.readdouble (); } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel dest,intflags) {Dest.writelong (addresult);34.            Dest.writelong (Subresult);            Dest.writelong (Mulresult);          Dest.writedouble (Divresult); }         Public Static FinalParcelable.creator<allresult> Creator =NewParcelable.creator<allresult>(){         Publicallresult createfromparcel (Parcel Parcel) {return NewAllresult (parcel); }          PublicAllresult[] NewArray (intsize) {            return NewAllresult[size];   }         }; } 

Allresult class inherits from Parcelable
Support for instantiating Allresult content through parcel objects is the reading order of the constructors.
Writetoparcel () is a "package" function that writes the data inside the Allresult class to parcel objects in a specific order, and the order in which they are written must match the reading order of the constructor
Static public field creator, used to construct Allresult objects using parcel objects.

We can write the following code in other classes:

long Addrusult = a + b;     long Subresult = a- b;     long mulresult = a * b;     double divresult = (double) A/(double) b;     New Allresult (Addrusult, Subresult, Mulresult, Divresult);

We can pass result. The Addresult variable gets the result of its operation.

Aidl and Service

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.