封装成库
我们现在已经实现了基本的通讯,但是每次我们改动的时候需要客户端同步改动的文件,如果用的客户端很多或者改动的文件很多就需要每个都改变,很麻烦,所以我们将客户端封装成一个库,提供给其他应用,如果有改动只需要提供一个库文件即可。
首先我们用我们的服务端程序开始改动。
在工程中创建一个库的目录。
创建好之后,像客户端那样将服务端文件复制到这个目录中,包括目录路径。
然后创建一个MyLib的类,写一些库的接口,实现就是将aidl接口进行一层封装。
唯一不同的是添加ConnectedListener接口,这个接口是用来通知应用库已经连接上服务了。
因为在我们直接写的时候是自己去进行服务的绑定,所以可以知道什么时候绑定完成,绑定完成后可以使用aidl对象中的方法。但是我们封装成库之后服务绑定是在库中实现的,就需要库使用接口的回调方式通知应用。
package com.example.mylibrary;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.myapplication.IClientCallBack;
import com.example.myapplication.IMyAidlInterface;
import com.example.myapplication.User;
public class MyLib {
private IMyAidlInterface iMyAidlInterface;
private ConnectedListener connectedListener;
public interface ConnectedListener {
// 回调方法
void LibConnected();
}
public void setOnConnectedListener(ConnectedListener connectedListener) {
this.connectedListener = connectedListener;
}
public MyLib(Context context) {
Intent intent = new Intent();
intent.setPackage("com.example.myapplication");
intent.setAction("com.example.aidlservice.action");
context.bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
if (connectedListener != null)
connectedListener.LibConnected();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Context.BIND_AUTO_CREATE);
}
public String getName() {
try {
return iMyAidlInterface.getName();
} catch (RemoteException e) {
e.printStackTrace();
}
return null;
}
public User getUser() {
try {
return iMyAidlInterface.getUser();
} catch (RemoteException e) {
e.printStackTrace();
}
return null;
}
public void registerCallBack(IClientCallBack cb) {
try {
iMyAidlInterface.register(cb);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
在这一步骤中需要添加对app的依赖,只要按照studio提示进行点击就可以了,他会帮我们写好。
implementation project(path: ':app')
再现我们开始编译自己的库吧
完成过后会在目录MyApplication8\mylibrary\build\outputs\aar出现aar包,
mylibrary-debug.aar
这个aar包就我们后面需要用到的包。
下面我们来创建一个使用这个库的应用程序,这个程序也在这个工程中进行创建。方法与上面类似。
将上面生成的aar拷贝到MyApplication8\aidldemo\libs目录下。
我们按照之前客户端的方法写这个xml和activity。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.aidldemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import com.example.myapplication.IClientCallBack;
import com.example.mylibrary.MyLib;
import java.util.List;
public class MainActivity extends AppCompatActivity {
MyLib mMyLib;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv1 = findViewById(R.id.textview1);
TextView tv = findViewById(R.id.textview);
Button bt = findViewById(R.id.button);
mMyLib = new MyLib(this);
mMyLib.setOnConnectedListener(() -> mMyLib.registerCallBack(new IClientCallBack.Stub() {
@Override
public void update(List<String> list) {
tv1.setText("update");
}
}
));
bt.setOnClickListener(v -> {
tv.setText("" + mMyLib.getName());
});
Button bt1 = findViewById(R.id.button1);
bt1.setOnClickListener(v -> {
tv.setText("" + mMyLib.getUser().getName());
});
}
}
如何设置编译的应用你呢?
名字对应应用。
例子程序:https://gitee.com/RTplay/AidlDemo