轉載https://blog.csdn.net/u013347784/article/details/51604009
一、AIDL使用步驟(Android studio環境)
場景:應用B要調用應用A中的方法。
應用A中要做的事情:
(1)src下 右鍵創建一個aidl文件,提供一個打印日誌的方法,編碼方式類似java,如圖:
刷新或重新編譯一下會生成對應的java文件如圖:
(2)新建類,該類繼承Service類,類名可自定義。
package com.example.zhangshun.myapplication.aidla;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.zhangshun.myapplication.IMyAidlInterface;
/**
* Created by zhangshun on 2016/6/7.
*/
public class AIDLService extends Service {
private MyBinder myBinder;
/**
*在onBind中返回Binder對象
*/
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
/**
* 在onCreate中創建Binder對象
*/
@Override
public void onCreate() {
super.onCreate();
myBinder = new MyBinder();
}
/**
* 定義內部類繼承自AIDL自動生成的Stub類
* 該類繼承了 android.os.Binder 實現了 com.example.zhangshun.myapplication.aidl.IMyAidlInterface
*/
public class MyBinder extends IMyAidlInterface.Stub {
@Override
public void printLog(String s) throws RemoteException {
Log.i("ALDLService", "printLog: AIDLTEST -- "+s);
}
}
}
(3)在AndroidManifest.xml文件中配置service,注意action是自定義的是B應用要調用A應用的鑰匙,後面會用到
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhangshun.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AIDLService">
<intent-filter>
<action android:name="com.example.zhangshun.myapplication.IMyAidlInterface"/>
</intent-filter>
</service>
</application>
</manifest>
應用B要做的事情:
(1)將aidl文件拷貝過來
(2)綁定服務 獲取aidl接口 調用方法
package com.example.zhangshun.myapplication.aidlb;
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.util.Log;
import com.example.zhangshun.myapplication.IMyAidlInterface;
public class MainActivity extends Activity {
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService();
}
/**
* 綁定服務
*/
private void bindService(){
Intent intent = new Intent();
intent.setAction("com.example.zhangshun.myapplication.IMyAidlInterface");
//android 5.0以後的要求 必須明確指明調用的哪裏的服務
intent.setPackage("com.example.zhangshun.myapplication");
boolean is = bindService(intent, serviceConnection, BIND_AUTO_CREATE);
Log.i("AIDLService", "bindService: "+is);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// Stub.asInterface,獲取接口
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
iMyAidlInterface.printLog("aldl 測試");
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
}
服務綁定後獲取到 iMyAidlInterface之後可以在需要的地方調用接口裏面的方法,注意intent裏面的action不要寫錯!
完成!