AIDL call Guide

Source: Internet
Author: User

AIDL call Guide

Recently, there is a need to implement communication between two apks. I want to use AIDL. Now I want to write a demo to learn how to use AIDL.
Here I want to implement an apk (client Side) to call another apk (server side) method.

First, implement the server. The code structure is as follows:



The content of the AIDL file is as follows:

package com.example.testaidl;interface MyInterface {    void testMethod();}

MainActivity. java

package com.example.testaidlserver;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startService(new Intent(this, MyService.class));}}

MyService. java

package com.example.testaidlserver;import com.example.testaidl.MyInterface;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {Log.i("MyService", "service onBind....");return new ImplAIDLService();}private class ImplAIDLService extends MyInterface.Stub {@Overridepublic void testMethod() throws RemoteException {Log.i("MyService", "testMode invoked");}}}

Add MyService registration in Manifest

<service  android:name="com.example.testaidlserver.MyService">     <intent-filter>           <action android:name="com.example.testaidlserver.MyService"/>           <category android:name="android.intent.category.DEFAULT"/>     </intent-filter></service>

Below is the Client side


The aidl file and server must be the same

package com.example.testaidl;interface MyInterface {    void testMethod();}

The MainAcitvity function is to bind the server service and call the server method.

package com.example.testaidlclient;import com.example.testaidl.MyInterface;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;public class MainActivity extends Activity implements OnClickListener {private boolean mIsBinded = false;private MyInterface mInterface;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn_bind = (Button)findViewById(R.id.btn_bind);btn_bind.setOnClickListener(this);Button btn_invoke = (Button)findViewById(R.id.btn_invoke);btn_invoke.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()) {case R.id.btn_bind:if(mIsBinded) {unbindService(con);}else {bindService(new Intent("com.example.testaidlserver.MyService"), con, Context.BIND_AUTO_CREATE);}break;case R.id.btn_invoke:try {mInterface.testMethod();} catch (RemoteException e) {e.printStackTrace();}break;}}ServiceConnection con = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {mIsBinded = false;mInterface = null;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mIsBinded = true;mInterface = MyInterface.Stub.asInterface(service);}};}

Running result:



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.