Android跨进程通信二——AIDL
生活随笔
收集整理的這篇文章主要介紹了
Android跨进程通信二——AIDL
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
AIDL全稱Android Interface Definition Language即安卓接口定義語言。主要用于多進(jìn)程通信。比Messenger,它具有支持多線程優(yōu)勢
注意事項(xiàng):
為了線程安全考慮,服務(wù)端可以使用多線程處理到來的請求
客戶端發(fā)送給服務(wù)端的請求如果需要好幾毫秒處理時(shí)間,為了防止客戶端出現(xiàn)ANR(程序未響應(yīng))問題,則客戶端需要單獨(dú)開一個(gè)線程用于請求。
不要將線程拋回給請求方
配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.androidClient" ><application android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name="com.example.androidClient.MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".RemoteService"android:enabled="true"android:exported="true"android:process=":remote"></service></application></manifest>布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"android:orientation="vertical"><Button android:id="@+id/bind_service"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorPrimary"android:onClick="bind_service"android:text="綁定服務(wù)"/><Button android:id="@+id/unbind_service"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorPrimary"android:layout_marginTop="20dp"android:onClick="unbind_service"android:text="解除綁定"/><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:text="信息提示:"android:layout_marginTop="20dp"/><TextView android:id="@+id/message"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>定義AIDL文件
// IRemoteService.aidl package com.example.androidClient;// Declare any non-default types here with import statementsinterface IRemoteService {/*** 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);int getPid();int sum(int a, int b);}服務(wù)端
package com.example.androidClient;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; import android.os.RemoteException;/*** 服務(wù)端的服務(wù)*/ public class RemoteService extends Service {@Overridepublic void onCreate() {super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {return mBinder;}public IRemoteService.Stub mBinder = new IRemoteService.Stub() {@Overridepublic void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {}@Overridepublic int getPid() throws RemoteException {return Process.myPid();}@Overridepublic int sum(int a, int b) throws RemoteException {return a+b;}}; }客戶端
package com.example.androidClient;import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.*; import android.os.Process; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {TextView message;IRemoteService mService;private ServiceConnection mServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {mService = IRemoteService.Stub.asInterface(service);Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();try {message.setText("Caller Process ID: "+ Process.myPid()+"\n" +"Service Process ID: "+mService.getPid()+"\n"+"Result: "+mService.sum(2,3));} catch (RemoteException e) {e.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {mService = null;Toast.makeText(MainActivity.this,"failure",Toast.LENGTH_SHORT).show();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);message = (TextView) findViewById(R.id.message);}public void bind_service(View view){/*Intent intent = new Intent();intent.setAction("com.example.android.remote_service");intent.setPackage("com.example.android");*/Intent intent = new Intent(this,RemoteService.class);bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);}public void unbind_service(View view){unbindService(mServiceConnection);} }效果圖
內(nèi)容來自:https://developer.android.com/guide/components/aidl.html
總結(jié)
以上是生活随笔為你收集整理的Android跨进程通信二——AIDL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开源框架——事件总线 Ev
- 下一篇: AIDL注意细节 简单Demo