Use of the four components of the Android application aidl How to implement a cross process call service_android

Source: Internet
Author: User
Tags sqlite database stub

I. Description of the problem

The four components of the Android application are activity, Broadcastreceiver, ContentProvider, and service that can be performed across processes. In the previous article we implemented a contentprovider call between different applications, but ContentProvider is primarily a sharing of data (such as a SQLite database), so what do we want to call services across the process (service)? The Android system is implemented using a remote procedure call (RPC) approach. Like many other RPC-based solutions, Android exposes service interfaces using an Interface definition language (Interface definition Language,idl). For services across the process call needs to be achieved through AIDL, Aidl Service application is very wide, such as the Baidu Map API, to provide services across the process, we will look at how to achieve the Aidl service, case map:

Ii. steps to achieve AIDL service

1. Preparation of AIDL documents

2. If the contents of the Aidl file are correct, a Java interface file (*.java) is automatically generated.

3. Create a service class (a subclass of services).

4. Implement the Java interface generated by the Aidl file.

5. Configure the Aidl service in the Androidmanifest.xml file and add the android:name of the <action> tag so that the client can start the service with an implicit intent

6. Client

Iii. Preparation of Aidl documents

Android Interface Definition Language--localservice.aidl

Package com.jereh.remote;
Interface localservice{
    String getlocal ();
}

The IDE automatically generates the Localservice.java file as shown in the figure:

Four, remote application implementation

  1, the preparation of Myremoteservice

public class Myremoteservice extends Service {
  @Override public
  ibinder onbind (Intent arg0) {
    //TODO Auto-ge nerated method Stub return
    new Myremoteserviceimpl ();
  }
  Private class Myremoteserviceimpl extends localservice.stub{
    @Override public
    String getlocal () throws remoteexception {
      //TODO auto-generated method stub return
      "Yantai Jerry Education";
    }
  }


2, Androidmanifest.xml Configuration

<service android:name= "Com.jereh.retmote.MyRemoteService"
      android:process= "remote"
      >
      < intent-filter>
        <action android:name= "Com.jereh.remote_service"/>
      </intent-filter>
 </service>

V. Client-side implementation

  1. Add Localservice.aidl to client application

Note that the package name is the same as the package name that is defined on the server side of the file. As shown in the figure:

Localservice.java code is also automatically generated

  2, Mainactivity code:

public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
    . OnCreate (Savedinstancestate);
  Setcontentview (R.layout.activity_main);
    public void StartService (view view) {Intent service=new Intent ("Com.jereh.remote_service");
  Super.bindservice (SERVICE, Conn, context.bind_auto_create);
      public void Showinfo (view view) {try {local=service.getlocal ();
      LOG.D ("Jereh", local);
    Toast.maketext (This, "You have entered" +local,toast.length_long). Show ();
    catch (RemoteException e) {//TODO auto-generated catch block E.printstacktrace ();
} private LocalService Service;
Private String Local;  Private serviceconnection conn=new serviceconnection () {@Override public void onservicedisconnected (componentname arg0) {} @Override public void onserviceconnected (componentname name, IBinder binder) {//TODO auto-g enerated method Stub//Get Onbinde of remote serviceR method returns the object proxy Service=localservice.stub.asinterface (binder);
  }
  }; @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Bar if
    It is present.
    Getmenuinflater (). Inflate (R.menu.main, menu);
  return true;

 }
}

XML file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android:orientation= "vertical"
  tools:context= ". Mainactivity ">
  <button
    android:layout_width=" match_parent "
    android:layout_height=" Wrap_ Content "
    android:text=" Start remote Service "android:onclick=" StartService "/>
   <button
    android:layout_ Width= "Match_parent"
    android:layout_height= "wrap_content"
    android:text= "View Information" android:onclick= " Showinfo "/>
</LinearLayout>

The above is described in this article to introduce you to the Android application of the four components of the use of Aidl how to implement a cross process call service, I hope you like.

Related Article

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.