android使用aidl原理
参考链接: https://www.twle.cn/l/yufei/android/android-basic-service-aidl.html
通过这部分代码, 加深对Android AIDL的理解
aidl server端
ILanguage.aidl
步骤: 在main目录下创建
aidl
文件夹, 并创建对应的包com.example.aidl
, 然后创建ILanguage.aidl
文件, 然后执行android studio的 Build->Make Project, 生成aidl的编译文件(这一步不可缺少)
// ILanguage.aidl
package com.example.aidl;
interface ILanguage {
String queryLanguage(int num);
}
MsLanguageService.java
package com.example.aidl_server;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.aidl.ILanguage;
public class MsLanguageService extends Service {
private IBinder binder = new LanguageQueryBinder();
private String[] names = {"python", "php", "java", "kotlin", "c", "swift"};
private String query(int num) {
if (num > 0 && num < 6) {
return names[num - 1];
}
return null;
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("**********执行Server端的OnBind()方法*********");
return binder;
}
private final class LanguageQueryBinder extends ILanguage.Stub {
@Override
public String queryLanguage(int num) throws RemoteException {
return query(num);
}
}
}
AndroidManifest.xml
清单文件中注册服务
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aidl_server">
<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/Theme.ComprehensiveExerciseTest">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册服务-->
<service
android:name=".MsLanguageService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.aidl_server.MsLanguageService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.aidl_server"
minSdkVersion 24
// 将默认版本(30)改成24,否则一直报错, 这里只是为了理解aidl的原理
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
代码结构图
aidl client端
同服务端一样创建
ILanguage.aidl
ILanguage.aidl
// ILanguage.aidl
package com.example.aidl;
interface ILanguage {
String queryLanguage(int num);
}
MainActivity.java
package com.example.aidl_client;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.aidl.ILanguage;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText ipt_search;
private Button btn_query;
private TextView txt_show;
private ILanguage iLanguage;
private LanguageConnection conn = new LanguageConnection();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
//绑定远程Service
Intent service = new Intent("com.example.aidl_server.MsLanguageService");
// service.setPackage("com.example.aidl_server");
service.setPackage("com.example.aidl_server");
boolean b = bindService(service, conn, BIND_AUTO_CREATE);
// boolean b = getApplicationContext().bindService(service, conn, BIND_AUTO_CREATE);
// System.out.println("-----------------------------");
// System.out.println(service);
// System.out.println(b);
// System.out.println("-----------------------------");
btn_query.setOnClickListener(this);
}
private void bindViews() {
ipt_search = (EditText) findViewById(R.id.ipt_search);
btn_query = (Button) findViewById(R.id.btn_query);
txt_show = (TextView) findViewById(R.id.txt_show);
}
@Override
public void onClick(View v) {
String number = ipt_search.getText().toString();
int num = Integer.valueOf(number);
System.out.println("+++++++++++++++++++++++++++++");
System.out.println(iLanguage);
System.out.println("+++++++++++++++++++++++++++++");
try {
txt_show.setText(iLanguage.queryLanguage(num));
} catch (RemoteException e) {
e.printStackTrace();
}
ipt_search.setText("");
}
private final class LanguageConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder service) {
iLanguage = ILanguage.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName name) {
iLanguage = null;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要查询的语言" />
<EditText
android:id="@+id/ipt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" />
<TextView
android:id="@+id/txt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.aidl_client"
minSdkVersion 24
//同aidl服务端一样,将版本改成24
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}