Simple use of Aidl

Source: Internet
Author: User

Simple use of Aidl

1 aidl (Android Interface definition Language): interface Definition language.
2 When you need to pass objects between different processes, such as passing objects between different applications, you need to use Aidl.
3 aid enables an application to invoke services from another application.

First, the server application (in fact, a service application) 1 Create a Aidl file

The development tools used here are Android Studio, create the Aidl folder under the main folder, and the Java folder sibling, create the package in the Aidl folder, and the package name must match the application package name. Finally create the Aidl file: Peopleserviceaidl.aidl

package com.miquan.demo;// AIDL其实就是一个接口,定义了通讯的规则。interface PeopleServiceAIDL {    String findPeople(int num);}

Click Build > Make Module xxx in the Android Studio menu, and a corresponding class will be generated. Specific location: Build > Generated > Source > Aidl > Debug > XXX

2 Creating a Service

Peopleservice.java

 PackageCom.miquan.demo;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException; Public  class peopleservice extends Service {    PrivateIBinder Mbinder =NewPeopleservicebinder (); Public Peopleservice() {    }@Override     PublicIBinderOnbind(Intent Intent) {returnMbinder; }Private Final  class peopleservicebinder extends peopleserviceaidl. Stub {        @Override         PublicStringFindpeople(intNumthrowsremoteexception {returnnum = =0?"Wang Nima":"Tangmaru"; }    }}

Configure the service in the manifest file (with an implicit start):

<service android:name=".PeopleService">    <intent-filter>        <action android:name="com.miquan.demo.people.service"/>    </intent-filter></service>

At this point the server is finished.

II. client (caller side)

The first thing to do is to create the Aidl file in the same way, or copy it to correspond.

 PackageCOM.MIQUAN.AS3;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Intent;ImportAndroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportCom.miquan.demo.PeopleServiceAIDL; Public  class mainactivity extends Activity {    PrivateButton Mbutton;PrivatePeopleserviceaidl Mpeopleserviceaidl;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mbutton = (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {Try{//The name obtained here is obtained from a service in another application.String name = Mpeopleserviceaidl.findpeople (0); }Catch(RemoteException e)                {E.printstacktrace ();        }            }        }); Bindservice (NewIntent ("Com.miquan.demo.people.service"), Mserviceconnection, bind_auto_create); }PrivateServiceconnection mserviceconnection =NewServiceconnection () {@Override         Public void onserviceconnected(componentname name, IBinder service)        {mpeopleserviceaidl = PeopleServiceAIDL.Stub.asInterface (service); }@Override         Public void onservicedisconnected(componentname name) {Mpeopleserviceaidl =NULL; }    };@Override    protected void OnDestroy() {Super. OnDestroy ();    Unbindservice (mserviceconnection); }}

Simple use of Aidl

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.