Cross-process Service calling (AIDL Service)

Source: Internet
Author: User

Cross-process Service call:

Sometimes we use some methods to call services in other applications or system applications in our own applications. In android, how can we call methods in Service across application processes.

The AIDL service is used below. Here is a brief introduction to AIDL:

AIDL: Android Interface Definition Language, which is the Android Interface Definition Language. Between processes in the Android system

Shared Memory. Therefore, you need to provide some mechanisms for data communication between different processes. To enable other applications to access the services provided by this application, the Android system uses Remote Procedure Call and RPC. Like many other RPC-based solutions, Android uses an Interface Definition Language (IDL) to publish service interfaces. We know that three of the four Android application components, namely Activity, BroadcastReceiver, and ContentProvider, can be accessed across processes. The Service of another Android application component can also be used. Therefore, the service that can be accessed across processes is called the AIDLAndroid Interface Definition Language) service.

 

Creating an AIDL service is more complex than creating a common service. The specific steps are as follows:
1) Create an aidl file in the Java package directory of the Eclipse Android project. The syntax of this file is similar to Java code, but it will be slightly different.
2) If the content of the aidl file is correct, ADT will automatically generate a Java interface file *. java ).
3) create a subclass of a Service class ).
4) Implement the Java interface generated by the aidl file.
5) in AndroidManifest. configure the AIDL service in the xml file. Note that the value of the android: name attribute in the <action> tag is the ID of the service to be referenced by the client, that is, the parameter value of the Intent class.

 

Next we will use an instance to explain how to call the Service across processes.

Requirement: In the input box, enter a number from 1 to 5, 1: Zhang San, 2: Li Si, 3: Wang Wu, 4: Zhao Liu, 5: Tian Qi. Enter 1, click the query button, and "Zhang San" will be displayed on TextView, and so on.

 

:

650) this. width = 650; "title =" aidl.png "src =" http://www.bkjia.com/uploads/allimg/140110/0426092922-0.jpg "alt =" wKioL1LLteWiT9fSAABETrxfFcw037.jpg "/>

 

Ideas:

1. Create an android project, write the layout file, find the corresponding control in the activity, and set the corresponding click events for each control.

2. Create another Service project without the main interface, but there is no activity set here.

3. There is no modifier for creating an interface under the Service project), in which we can write the abstract method we need.

package cn.cbd.personservice.adil;interface PersonServiceAidl{String getPersonInfo(int num);}

4. find this interface in the Service Project in the working directory and add the suffix. java. aidl then returns eclipse or ADT) refresh the project. We can see that a PersonServiceAidl is automatically generated under the gen directory. java file.

650) this. width = 650; "title =" aidl1.png "src =" http://www.bkjia.com/uploads/allimg/140110/0426092550-1.jpg "alt =" wKiom1LLueODvJzPAAAclptL0Kc041.jpg "/>

5. Copy the entire cn. cbd. personservice. adil package to the rec directory of another application, and refresh the project.

 

Note:

When configuring remote services in the configuration file, you must add

<Service android: name = ". PersonService"> <intent-filter> <! -- In the remote service, such an action is required --> <action android: name = "aaa. bbb. ccc"/> </intent-filter> </service>

Otherwise, an error is reported, where "<action android: name =" aaa. bbb. ccc "/>" is customized.

 

The following are some content in the main Acitivity:

Package cn. cbd. person; import android. app. activity; import android. content. componentName; import android. content. context; 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. editText; import android. widget. textView; import cn. cbd. personservice. adil. personServiceAidl; public class PersonActivity extends Activity {private EditText et_main; private Button btn_query; private TextView TV _showName; private PersonServiceAidl aidl; private MyServiceConnection conn; private Intent service; // PersonServicepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); et_main = (EditText) findViewById (R. id. et_main); btn_query = (Button) findViewById (R. id. btn_query); TV _showName = (TextView) findViewById (R. id. TV _showName); // create a MyServiceConnection object connconn = new MyServiceConnection (); // service = new Intent ("aaa. bbb. ccc "); service = new Intent (); service. setClassName ("cn. cbd. personservice "," cn. cbd. personservice. personService "); // enable the Service // this. startService (service); // bind the service this. bindService (service, conn, Context. BIND_AUTO_CREATE); btn_query.setOnClickListener (new OnClickListener () {public void onClick (View v) {String num = et_main.getText (). toString (). trim (); // call the try {String name = aidl method in the service. getPersonInfo (Integer. valueOf (num); TV _showName.setText (name);} catch (NumberFormatException e) {e. printStackTrace ();} catch (RemoteException e) {e. printStackTrace () ;}}) ;}@ Overrideprotected void onDestroy () {this. unbindService (conn); // this. stopService (service); super. onDestroy ();} public class MyServiceConnection implements ServiceConnection {public void onServiceConnected (ComponentName, IBinder service) {aidl = PersonServiceAidl. stub. asInterface (service);} public void onServiceDisconnected (ComponentName name ){}}}

 

From the above we can see that when we successfully bind a service through bindService (service, conn, flag), we will execute onServiceConnected (ComponentName, IBinder service) in the Implementation class MyServiceConnection class) method. In this method, we can obtain the object aidl = PersonServiceAidl. Stub. asInterface (service);) that operates the methods in the service.

 

The source code of the project package is provided below. If you do not understand it, you can check it yourself.

After decompression, deploy the project PersonService first, and then deploy the project Person.

 

This article is from the "SEVEN" blog, please be sure to keep this source http://930307.blog.51cto.com/7950022/1349494

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.