Android AIDL實現(xiàn)進程間通信探索
前言:
前面總結(jié)了程序間共享數(shù)據(jù),可以使用ContentProvider也可以使用SharedPreference,那么進程間怎么共享內(nèi)存呢?Android系統(tǒng)中的進程之間不能共享內(nèi)存,因此,需要提供一些機制在不同進程之間進行數(shù)據(jù)通信。
為了使其他的應(yīng)用程序也可以訪問本應(yīng)用程序提供的服務(wù),Android系統(tǒng)采用了遠程過程調(diào)用(Remote Procedure Call,RPC)方式來實現(xiàn)。與很多其他的基于RPC的解決方案一樣,Android使用一種接口定義語言(Interface Definition Language,IDL)來公開服務(wù)的接口。我們知道4個Android應(yīng)用程序組件中的3個(Activity、BroadcastReceiver和ContentProvider)都可以進行跨進程訪問,另外一個Android應(yīng)用程序組件Service同樣可以。因此,可以將這種可以跨進程訪問的服務(wù)稱為AIDL(Android Interface Definition Language)服務(wù)。
接下來實戰(zhàn)一下具體實現(xiàn):
1.)首先新建一個aidl文件
interface ITestInterface {
//獲取進程ID
int getProcessId();
//處理字符串
String dealString( String srcString);
//字符串追加
String appendString( String srcString);
void addPerson(in Person person);
List<Person> getPersons();
}
aidl語法解說:
• 聲明函數(shù)基本和Java一致,可以傳參和返回值,參數(shù)和返回值
• 參數(shù)和返回值 Java編程語言的基本數(shù)據(jù)類型 (int, long, char, boolean等),String和CharSequence,集合接口類型List和Map、其他AIDL接口類型、實現(xiàn)Parcelable接口的自定義對象
• 方向指示 在使用aidl傳輸數(shù)據(jù)時,對于非基本數(shù)據(jù)類型,也不是String和CharSequence類型的,(即Parcelable類型)需要有方向指示,包括in、out和inout。
• AIDL只支持接口方法,不能公開static變量。
2.)服務(wù)端實現(xiàn)接口
private final ITestInterface.Stub mBinder = new ITestInterface.Stub() {
public int getProcessId(){
Log.e("TestService","TestService Thread: " + Thread.currentThread().getName());
Log.e("TestService","TestService getProcessId()");
return android.os.Process.myPid();
}
//處理字符串
public String dealString( String srcString)
{
return srcString+srcString;
}
//字符串追加
public String appendString( String srcString)
{
return srcString+srcString;
}
3.)客戶端獲取接口
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("TestService","TestService onServiceDisconnected()");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iTestInterface = ITestInterface.Stub.asInterface(service);
try {
Log.e("TestService","TestService onServiceConnected()");
int remoteId=iTestInterface.getProcessId();
Log.e("TestService","TestService remoteId---->"+remoteId);
int currentPid = android.os.Process.myPid();
Log.e("TestService","TestService currentPid---->"+currentPid);
Log.e("TestService","TestService dealString---->"+iTestInterface.dealString("Remote Service"));
Log.e("TestService","TestService appendString---->"+iTestInterface.appendString("Remote Service"));
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
4.)通過IPC調(diào)用/傳遞數(shù)據(jù)
int remoteId=iTestInterface.getProcessId();
Log.e("TestService","TestService remoteId---->"+remoteId);
int currentPid = android.os.Process.myPid();
Log.e("TestService","TestService currentPid---->"+currentPid);
Log.e("TestService","TestService dealString---->"+iTestInterface.dealString("Remote Service"));
Log.e("TestService","TestService appendString---->"+iTestInterface.appendString("Remote Service"));
5.)Service聲明以及綁定/解綁
聲明:
<service
android:name=".TestService"
android:enabled="true"
android:exported="true"
android:label="remoteService"
android:process=":remote">
<intent-filter android:priority="1000">
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.whoislcj.testaidl.TestService" />
</intent-filter>
</service>
綁定:
Intent intent = new Intent("com.whoislcj.testaidl.TestService");
intent.setPackage(getPackageName());//這里你需要設(shè)置你應(yīng)用的包名
bindService(intent, connection, Context.BIND_AUTO_CREATE);
解綁:unbindService(connection);
6.)訪問權(quán)限同Service一致
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AndroidStudio4.0 New Class的坑(小結(jié))
這篇文章主要介紹了AndroidStudio4.0 New Class的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Android仿iOS實現(xiàn)側(cè)滑返回功能(類似微信)
這篇文章主要為大家詳細介紹了Android仿iOS實現(xiàn)側(cè)滑返回功能,類似微信功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
當前比較成熟一點的應(yīng)用基本上都會在進入應(yīng)用之顯示一個啟動界面.這個啟動界面或簡單,或復雜,或簡陋,或華麗,用意不同,風格也不同2013-04-04
Android仿微信底部實現(xiàn)Tab選項卡切換效果
這篇文章主要為大家介紹了Android仿微信底部實現(xiàn)Tab選項卡切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-02
Android應(yīng)用程序中讀寫txt文本文件的基本方法講解
這篇文章主要介紹了Android應(yīng)用程序中讀寫txt文本文件的基本方法講解,基本上依靠context.openFileInput()和context.openFileOutput()兩個方法為主,需要的朋友可以參考下2016-04-04
Android使用Scrolling機制實現(xiàn)Tab吸頂效果
app 首頁中經(jīng)常要實現(xiàn)首頁頭卡共享,tab 吸頂,內(nèi)容區(qū)通過 ViewPager 切換的需求,以前往往是利用事件處理來完成,但是這些也有一定的弊端和滑動方面不如意的地方,本文我們利用NestedScrolling機制來實現(xiàn),感興趣的朋友可以參考下2024-01-01

