Android programming to implement AIDL (cross process Communication) method detailed _android

Source: Internet
Author: User
Tags int size stub

The example in this article describes the approach of Android programming to implement Aidl (cross process communication). Share to everyone for your reference, specific as follows:

I. Overview:

Cross-process communication (AIDL), which mainly implements data sharing between processes (applications).

Two. Implementation process:

1. Server-side implementation:

(1) directory structure, as shown below:

(2) Implement *.aidl file:

A. IAIDLSERVICE.AIDL implementation:

Package com.focus.aidl;
Import Com.focus.aidl.Person;
Interface Iaidlservice {
  String getName ();
  Person Getperson ();
}

B. PERSON.AIDL implementation:

Parcelable person;

(3) Transitive objects between processes must implement parcelable or serializable interfaces, and the following is the implementation of the passed person object:

 package com.focus.aidl; import android.os.Parcel; import android.os.Parcelable; public
  Class Person implements parcelable {private String name;
  private int age;
    Public person () {} public person (Parcel source) {name = Source.readstring ();
  Age = Source.readint ();
  Public String GetName () {return name;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;
  public void Setage (int age) {this.age = age;
  public int describecontents () {return 0;
    public void Writetoparcel (Parcel dest, int flags) {dest.writestring (name);
  Dest.writeint (age); public static final parcelable.creator<person> Creator = new creator<person> () {public person[] Newar
    Ray (int size) {return new person[size];
    Createfromparcel (Parcel source) {return new person (source);
}
  }; }

(4) Implement the interface defined in the Iaidlservice.aidl file and define the service to return this implementation class when the service is bind:

Package com.focus.aidl;
Import com.focus.aidl.IAIDLService.Stub;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import android.os.RemoteException;
public class Aidlserviceimpl extends Service {
  @Override public
  ibinder onbind (Intent Intent) {
    return Mbinder;
  }
  /**
   * The interface implementation defined in the Aidl file.
   *
  private iaidlservice.stub Mbinder = new Stub () {public
    String getName () throws RemoteException {
   
    return "Mayingcai";
    }
    Public person Getperson () throws RemoteException {person
      Mperson = new Person ();
      Mperson.setname ("Mayingcai");
      Mperson.setage (in);
      Return Mperson}
    }
  ;
}


   

(5) Registering the service in the Androidmanifest.xml file:

<service android:name = ". Aidlserviceimpl "android:process =": Remote ">
  <intent-filter>
    <action android:name =" Com.focus.aidl.IAIDLService "/>
  </intent-filter>
</service>

2. Client implementation:

(1) directory structure, as shown below:

(2) Copy server-side iaidlservice.aidl,person.aidl and Person.java files to this project, as shown above:

(3) Res/layout/main.xml implementation:

 <?xml version=" 1.0 "encoding=" Utf-8 "?> <linearlayout =" xmlns:android Schemas.android.com/apk/res/android "android:orientation =" vertical "Android:layout_width =" Fill_parent "android:l
    Ayout_height = "Fill_parent" > <textview android:id = "@+id/name" android:layout_width = "Wrap_content" Android:layout_height = "Wrap_content"/> <button android:id = "@+id/connection" Android:layout_wi DTH = "Wrap_content" android:layout_height = "wrap_content" Android:text = "Connection"/> <button Android : id = "@+id/message" android:layout_width = "wrap_content" android:layout_height = "Wrap_content" Android:enab LED = "false" Android:text = "Information"/> <button android:id = "@+id/person" android:layout_width = "WR" Ap_content "android:layout_height =" wrap_content "android:enabled =" false "Android:text =" Person "/> < /linearlayout> 

(4) host activity implementation, from the server side to obtain data on the client display:

Package com.focus.aidl.client;
Import android.app.Activity;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Com.focus.aidl.IAIDLService;
Import Com.focus.aidl.Person;
  public class Aidlclientacitivty extends activity {private Iaidlservice maidlservice;
  Private TextView Mname;
  Private Button mmessage;
  Private Button Mperson;
   /** * The first step is to create the Serviceconnection object and obtain the Iaidlservice implementation in the Onserviceconnected () method. * Private Serviceconnection Mserviceconnection = new Serviceconnection () {public void onserviceconnected (Component
      Name name, IBinder service) {Maidlservice = IAIDLService.Stub.asInterface (service);
      Mmessage.setenabled (TRUE);
    Mperson.setenabled (TRUE); } publicvoid onservicedisconnected (componentname name) {maidlservice = null;
      Mmessage.setenabled (FALSE);
    Mperson.setenabled (FALSE);
  }
  };
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Mname = (TextView) Findviewbyid (r.id.name); Findviewbyid (r.id.connection). Setonclicklistener (New Onclicklistener () {public void OnClick (view view) {/*
         * * Second step, click the "Connect" button and use Mserviceconnection to bind the service created by the server side.
        * * Intent service = new Intent ("Com.focus.aidl.IAIDLService");
      Bindservice (service, mserviceconnection, bind_auto_create);
    }
    });
    Mmessage = (Button) Findviewbyid (r.id.message); Mmessage.setonclicklistener (New Onclicklistener () {public void OnClick (view view) {/** * Step three, from the server
         The end gets the string.
        * * try {mname.settext (Maidlservice.getname ());
    catch (RemoteException e) {      E.printstacktrace ();
    }
      }
    });
    Mperson = (Button) Findviewbyid (R.id.person); Mperson.setonclicklistener (New Onclicklistener () {public void OnClick (view view) {/** * Fourth step, from server side
         Gets the person object.
          * * try {person Mperson = Maidlservice.getperson ();
        Mname.settext ("Name:" + mperson.getname () + ", Age:" + mperson.getage ());
        catch (RemoteException e) {e.printstacktrace ();
  }
      }
    });

 }
}

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.