AIDL調用指南

來源:互聯網
上載者:User

AIDL調用指南

最近有需求要實現兩個apk之間的通訊,想到用AIDL來實現,現寫一個demo學習下AIDL如何使用。
這裡我要實現一個apk(client端)調用另一個apk(server端)的方法.

先實現server端,代碼結構如下



AIDL檔案內容如下:

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");}}}

Manifest中添加MyService的註冊

<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>

下面是Client端的


aidl檔案和server端的要一樣

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

MainAcitvity的功能是,綁定server端的service,調用server端的方法

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);}};}

運行結果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.