一、 AIDL简介
AIDL一般是Service端提供给Client端使用的,通过binder将进程通信实现得像函数调用一样。比如说某个app要对接另外一个app的服务,就需要使用另外一个app提供的aidl文件,在服务启动时获取远程服务的引用,从而调用远程服务的接口。
二、 AIDL Demo
按照逻辑首先编写aidl服务端,也就是我需要提供接口给其他app。创建默认app后,不需要Service app去启动app,因为到处Service后就可以用client app去启动Service。
Service app manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aidlservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
然后创建aidl文件
// IMyService.aidl
package com.example.aidlservice;
// Declare any non-default types here with import statements
interface IMyService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
String getParam(String key);
}
Android studio会自动生成相应的java文件,不过并不需要我们去关注。
接下来创建Service文件
package com.example.aidlservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
private IBinder iBinder;
public MyService() {
iBinder=new IMyService.Stub(){
@Override
public String getParam(String key) throws RemoteException {
Log.d("test","getparam");
if(key!=null && key.equalsIgnoreCase("name")){
return "xiaoshixiu";
}
return null;
}
};
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return iBinder;
}
}
接下来创建client app。
Client首先要引入aidl文件,不能改变包名和文件名。
// IMyService.aidl 注意兩個app的aidl文件包名和文件名必須要一樣
package com.example.aidlservice;
// Declare any non-default types here with import statements
interface IMyService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
String getParam(String key);
}
然后修改MainActivety的逻辑
package com.example.aidlservice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private IMyService myService;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService=IMyService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.example.aidlservice","com.example.aidlservice.MyService"));//当service没设置receiver时可以直接设置Component启动service
bindService(intent,connection,BIND_AUTO_CREATE);
}
public void onclickbtn(View view){
Toast.makeText(this,"test",Toast.LENGTH_LONG);
try{
((TextView)findViewById(R.id.tv_dis)).setText(myService.getParam("name"));
}catch (Exception e){
e.printStackTrace();
}
}
}
layout文件很简单,就是点击按钮后显示服务名
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv_dis"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn"
android:onClick="onclickbtn"/>
</LinearLayout>
好了,我们来运行一下
Demo 链接:https://download.csdn.net/download/xiaoshixiu/12379375