詳解Android aidl的使用方法
AIDL是Android中IPC(Inter-Process Communication)方式中的一種,AIDL是Android Interface definition language的縮寫(對于小白來說,AIDL的作用是讓你可以在自己的APP里綁定一個其他APP的service,這樣你的APP可以和其他APP交互。)
AIDL只是Android中眾多進程間通訊方式中的一種方式,
AIDL和Messenger的區(qū)別:
- Messenger不適用大量并發(fā)的請求:Messenger以串行的方式來處理客戶端發(fā)來的消息,如果大量的消息同時發(fā)送到服務端,服務端仍然只能一個個的去處理。
- Messenger主要是為了傳遞消息:對于需要跨進程調用服務端的方法,這種情景不適用Messenger。
- Messenger的底層實現(xiàn)是AIDL,系統(tǒng)為我們做了封裝從而方便上層的調用。
- AIDL適用于大量并發(fā)的請求,以及涉及到服務端端方法調用的情況
AIDL通信的原理:首先看這個文件有一個叫做proxy的類,這是一個代理類,這個類運行在客戶端中,其實AIDL實現(xiàn)的進程間的通信并不是直接的通信,客戶端和服務端都是通過proxy來進行通信的:客戶端調用的方法實際是調用是proxy中的方法,然后proxy通過和服務端通信將返回的結果返回給客戶端。
1、AIDL的作用
AIDL是用于Android的IPC通訊的,因此可以在一個APP內部通訊,也可以創(chuàng)建兩個APP之間進行通訊。
AIDL的職能分配很明確,Service作為后臺運行作為服務器管理各種交互,Client作為客戶端請求數(shù)據(jù)或調用Service的方法。
2、AIDL的簡單使用
1)創(chuàng)建一個aidl文件,直接右鍵創(chuàng)建就可以了,
package com.example.mytest;
// IMyAidlInterface.aidl
package com.example.mytest;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
String add(int x , int y);
}
2)選中剛剛建立的 .aidl文件 生產對應的java文件。
AndroidStudio 可以通過Build--》model App 完成
3)編寫Service的具體對象 實現(xiàn)接口
package com.example.mytest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class FirstService extends Service {
public FirstService() {
}
private static String Tag = "FirstService";
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
Log.d(Tag,"service on bind");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(Tag,"OnCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Tag,"onStartCommand");
return START_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(Tag,"onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(Tag,"onDestroy");
}
IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub(){
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String add(int x, int y) throws RemoteException {
Log.d(Tag,x + "--" + y);
return String.valueOf(x + y);
}
};
}
注意:onBund 返回IBinder類型,為了后面的回調會調動
4)確定一下AndroidManifest.xml里面的Service內容
<service
android:name=".FirstService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.mytest.aidl.FirstService"/>
</intent-filter>
</service>
5)打開服務
private Button btnStartService;
private Button btnBindService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvId = (TextView) findViewById(R.id.tv_id);
btnStartService = (Button) findViewById(R.id.btn_Start_Service);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setPackage("com.example.mytest");
intent.setAction("com.example.mytest.aidl.FirstService");
startService(intent);
}
});
btnBindService = (Button) findViewById(R.id.btnBindService);
btnBindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bind();
}
});
private void bind(){
Log.d(Tag, "bind");
Intent intent = new Intent();
intent.setPackage("com.example.mytest");
if(controllerConnection != null){
this.bindService(intent,controllerConnection,this.BIND_AUTO_CREATE);//綁定服務,建立鏈接
}
else {
Log.d(Tag, "controllerConnection != null");
}
}
private void unbind(){
if(controllerConnection != null && myAIDLController.asBinder().isBinderAlive()){
try{
Log.d(Tag, "this.unbindService(controllerConnection);");
this.unbindService(controllerConnection);
} catch (Exception localException) {
Log.w(Tag, "unbind Exception localException");
}
}
}
在bind的時候是異步的,因此可以通過onServiceConnected()來判斷綁定上后的操作。
private ServiceConnection controllerConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(Tag,"onServiceConnected");
myAIDLController = IMyAidlInterface.Stub.asInterface(iBinder);
if (myAIDLController != null) {
try {
Log.d(Tag, "ServiceConnection success");
Toast.makeText(MainActivity.this, "ServiceConnection success", Toast.LENGTH_LONG).show();
} catch (Exception localException) {
Log.w(Tag, "Exception localException");
}
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d(Tag,"onServiceDisconnected");
Toast.makeText(MainActivity.this, "onServiceDisconnected", Toast.LENGTH_LONG).show();
myAIDLController = null;
}
@Override
public void onBindingDied(ComponentName name) {
Log.d(Tag,"onBindingDied");
}
@Override
public void onNullBinding(ComponentName name) {
Log.d(Tag,"onNullBinding");
}
};
6)調用Service方法add()
調用的時候需要綁定Service方法,上面已經有了,接下來調用就簡單了,創(chuàng)建一個Button,然后拿到Service的控制對象,調用方法add
btnServiceFunc = (Button) findViewById(R.id.btnServiceFunc);
btnServiceFunc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Log.d(Tag, String.valueOf( myAIDLController.add(1,2)));
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
到此這篇關于詳解Android aidl的使用方法的文章就介紹到這了,更多相關Android aidl的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android優(yōu)化方案之Fragment的懶加載實現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Android?Flutter實現(xiàn)自由落體彈跳動畫效果
粒子運動是將對象按照一定物理公式進行的自定義軌跡運動,與普通動畫不同的是,它沒有強制性的動畫開始到結束的時間概念。本文將利用Flutter實現(xiàn)自由落體彈跳動畫效果,感興趣的小伙伴可以學習一下2022-10-10
Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下2013-04-04

