AIDL全称Android Interface Definition Language即安卓接口定义语言。主要用于多进程通信。比Messenger,它具有支持多线程优势
注意事项:
为了线程安全考虑,服务端可以使用多线程处理到来的请求
客户端发送给服务端的请求如果需要好几毫秒处理时间,为了防止客户端出现ANR(程序未响应)问题,则客户端需要单独开一个线程用于请求。
不要将线程抛回给请求方
配置文件
<?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="绑定服务"/>
<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 statements
interface 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);
}
服务端
package com.example.androidClient;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
/**
* 服务端的服务
*/
public class RemoteService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public IRemoteService.Stub mBinder = new IRemoteService.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public int getPid() throws RemoteException {
return Process.myPid();
}
@Override
public 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() {
@Override
public 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();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
Toast.makeText(MainActivity.this,"failure",Toast.LENGTH_SHORT).show();
}
};
@Override
protected 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);
}
}
效果图
内容来自:https://developer.android.com/guide/components/aidl.html