Simple use of AIDL and simple use of AIDL

Source: Internet
Author: User

Simple use of AIDL and simple use of AIDL
Simple use of AIDL

1 AIDL (Android Interface Definition Language): Interface Definition Language.
2. When objects need to be transferred between different processes, for example, when objects are transferred between different applications, AIDL is required.
3. AID enables one application to call the services of another application.

I. server application (actually an application providing services) 1. Create an AIDL File

The development tool used here is Android Studio. Create the aidl folder under the main folder, which is the same as the java folder. Create a package in the aidl folder. The package name must be consistent with the application package name, finally, create the aidl file PeopleServiceAIDL. aidl

Package com. miquan. demo; // AIDL is actually an interface that defines communication rules. Interface PeopleServiceAIDL {String findPeople (int num );}

Click Build> Make Module xxx in the menu of Android Studio, and a corresponding class will be generated. Location: build> generated> source> aidl> debug> xxx

2. Create a Service

PeopleService. java

Package com. miquan. demo; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. OS. remoteException; public class PeopleService extends Service {private IBinder mBinder = new PeopleServiceBinder (); public PeopleService () {}@ Override public IBinder onBind (Intent intent) {return mBinder ;} private final class PeopleServiceBinder extends PeopleServiceAIDL. st Ub {@ Override public String findPeople (int num) throws RemoteException {return num = 0? "Wang Nima": "Tang Maru ";}}}

Configure the Service in the configuration file (start with implicit ):

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

Now the server is complete.

Ii. Client (caller)

First, you must create or copy the aidl file in the same way.

Package com. miquan. as3; 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. widget. button; import com. miquan. demo. peopleServiceAIDL; public class MainActivity extends Activity {private Button mButton; private PeopleServiceAIDL mPeopleServiceAIDL; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {try {// The name obtained here is the String name = mPeopleServiceAIDL obtained from the service in another application. findPeople (0);} catch (RemoteException e) {e. printStackTrace () ;}}); bindService (new Intent ("com. miquan. demo. people. service "), mServiceConnection, BIND_AUTO_CREATE);} private ServiceConnection mServiceConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName, IBinder service) {mPeopleServiceAIDL = leleserviceaidl. stub. asInterface (service) ;}@ Override public void onServiceDisconnected (ComponentName name) {mPeopleServiceAIDL = null ;};@ Override protected void onDestroy () {super. onDestroy (); unbindService (mServiceConnection );}}

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.