业务需求:监听车辆状态(电池信息,soc,车速,挡位等等),和通过报文控制车辆,接受和控制写在一个单独的App里面(service)
1.服务器端App写ALDL接口文件
- bean类
package com.hopechart.core.autodriver;
// 703新能源车辆状态Bean
parcelable AutoDriverInfo;
- 车辆状态监听接口
package com.hopechart.core.autodriver;
//导入bean类
import com.hopechart.core.autodriver.AutoDriverInfo;
interface IAutoDriverStateListener {
void onAutoDriverInfoChanged(in AutoDriverInfo autoDriverInfo);
}
- 控制车辆接口和添加移除回调接口
package com.hopechart.core.autodriver;
import com.hopechart.core.autodriver.IAutoDriverStateListener;
interface IAutoDriverServiceInterface {
/**
* 描述 : 设置左转灯的状态
* 参数 : boolean isOpen
* 返回 : 无
*/
void setLeftLightSwitch(boolean isOpen);
/**
* 描述 : 设置左转灯的状态
* 参数 : boolean isOpen
* 返回 : 无
*/
void setRightLightSwitch(boolean isOpen);
/**
* 描述:设置车辆状态回调接口
* 参数:IAutoDriverStateListener listener
* 返回:无
*/
void addAutoDriverStateListener(IAutoDriverStateListener listener);
/**
* 描述:移除车辆状态回调
* 参数:IAutoDriverStateListener listener
* 返回:无
*/
void removeAutoDriverStateListener(IAutoDriverStateListener listener);
}
2.服务端app,Bean类和监听接口
- bean类
public class AutoDriverInfo implements Parcelable {
// 实现parcelable接口
}
- 监听状态变化接口
package com.hopechart.core.autodriver;
public interface AutoDriverStateListener {
default void onAutoDriverInfoChanged(AutoDriverInfo autoDriverInfo){}
}
3.服务端App实现
- Service
package com.hopechart.core.vehicle;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import com.hopechart.base.ICoreService;
import com.hopechart.core.autodriver.AutoDriverInfo;
import com.hopechart.core.autodriver.AutoDriverStateListener;
import com.hopechart.core.autodriver.IAutoDriverServiceInterface;
import com.hopechart.core.autodriver.IAutoDriverStateListener;
/**
* @author lg
* @ClassName AutoDriverService.java
* @Description 703无人驾驶
* @createTime 2021年11月09日
*/
public class AutoDriverService implements Service, AutoDriverStateListener {
private AutoDriverServiceBinder mAutoDriverServiceBinder;
private AutoDriverHelper mAutoDriverHelper;
// RemoteCallbackList 用于跨进程间监听回调,可以在多个监听间轮询
private RemoteCallbackList<IAutoDriverStateListener> remoteCallbackList = new RemoteCallbackList<>();
private static class AutoDriverServiceHolder {
private static AutoDriverService instance = new AutoDriverService();
}
private AutoDriverService() {
mAutoDriverHelper = new AutoDriverHelper();
mAutoDriverServiceBinder = new AutoDriverServiceBinder();
mAutoDriverHelper.setAutoDriverStateListener(this);
}
//启动该service
public static AutoDriverService getInstance() {
return AutoDriverServiceHolder.instance;
}
@Override
public IBinder onBind(Intent intent) {
return mAutoDriverServiceBinder;
}
//实现监听接口
@Override
public void onAutoDriverInfoChanged(AutoDriverInfo autoDriverInfo) {
// 开始监听
int n = remoteCallbackList.beginBroadcast();
for (int i = 0; i < n; i++) {
try {
remoteCallbackList.getBroadcastItem(i).onAutoDriverInfoChanged(autoDriverInfo);
} catch (RemoteException e) {
e.printStackTrace();
}
}
//移除监听
remoteCallbackList.finishBroadcast();
}
//继承aldl接口
private class AutoDriverServiceBinder extends IAutoDriverServiceInterface.Stub {
@Override
public void setLeftLightSwitch(boolean isOpen) throws RemoteException {
//这里是具体的实现逻辑,调用自己的方法
mAutoDriverHelper.setLeftLightSwitch(isOpen);
}
@Override
public void setRightLightSwitch(boolean isOpen) throws RemoteException {
//这里是具体的实现逻辑,调用自己的方法
mAutoDriverHelper.setRightLightSwitch(isOpen);
}
@Override
public void addAutoDriverStateListener(IAutoDriverStateListener listener) throws RemoteException {
//在车辆状态信息变化后,去开始监听
if (listener != null) {
remoteCallbackList.register(listener);
}
}
@Override
public void removeAutoDriverStateListener(IAutoDriverStateListener listener) throws RemoteException {
if (listener != null) {
remoteCallbackList.unregister(listener);
}
}
}
}
- 接收车辆状态变化的类
public class AutoDriverHelper {
private AutoDriverStateListener mListener;
if (mListener != null) {
//在车辆信息变化时去调用
mListener.onAutoDriverInfoChanged(autoDriverInfo);
}
}
4.客户端App
public class AutoDriverController extends Activity{
private IAutoDriverServiceInterface mAutoDriverService;
private List<AutoDriverStateListener> mListeners = new ArrayList<>();
private InternalAutoDriverStateListener internalAutoDriverStateListener;
/**
* @param isOpen
* @Description 设置左转灯状态
*/
public void setLeftLightSwitch(boolean isOpen) {
try {
mAutoDriverService.setLeftLightSwitch(isOpen);
} catch (RemoteException e) {
e.printStackTrace();
}
}
/**
* @param isOpen
* @Description 设置右转灯的状态
*/
public void setRightSwitch(boolean isOpen) {
try {
mAutoDriverService.setRightLightSwitch(isOpen);
} catch (RemoteException e) {
e.printStackTrace();
}
}
/**
* 描述:设置无人驾驶车辆信息回调接口
* 参数:AutoDriverStateListener listener
* 返回:无
*/
public synchronized void addAutoDriverStateListener(AutoDriverStateListener listener) {
if (!mListeners.contains(listener)) {
mListeners.add(listener);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent serviceIntent = new Intent();
//服务端app的service所在的包名
serviceIntent.setPackage("com.hopechart.core");
//服务端app的service的action(AndroidManifest)
serviceIntent.setAction("com.hopechart.core.service");
bindService(serviceIntent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IAutoDriverServiceInterface = IAutoDriverServiceInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Context.BIND_AUTO_CREATE);
}
/**
* 描述 : 移除空调状态回调,在需要监听车辆状态的地方调用
* 参数 : AutoDriverStateListener listener
* 返回 : 无
*/
public synchronized void removeAutoDriverStateListener(AutoDriverStateListener listener) {
if (mListeners.contains(listener)) {
mListeners.remove(listener);
}
}
//客户端需要开启线程监听状态变化
private class InternalAutoDriverStateListener extends IAutoDriverStateListener.Stub {
@Override
public void onAutoDriverInfoChanged(AutoDriverInfo autoDriverInfo) throws RemoteException {
synchronized (AutoDriverController.this) {
if (!mListeners.isEmpty()) {
for (AutoDriverStateListener listener : mListeners) {
runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onAutoDriverInfoChanged(autoDriverInfo);
}
});
}
}
}
}
}
}