AIDL进程间通信,服务端向客户端发送消息
// aidl文件
package com.zlw.aidl;
import com.zlw.aidl.IServiceCallback;
interface IRemoteService {
void registerCallback(IServiceCallback cb);
void unregisterCallback(IServiceCallback cb);
}
// aidl文件
package com.zlw.aidl;
interface IServiceCallback {
void valueChanged(boolean isChange);
}
// 服务端Service类
public class RemoteService extends Service {
private static final String TAG = "RemoteService ";
private static RemoteCallbackList<IServiceCallback > mCallbacks;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate()");
mCallbacks = new RemoteCallbackList<>();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind()");
return super.onUnbind(intent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind()");
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onRebind(Intent intent) {
Log.i(TAG, "onRebind()");
super.onRebind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy()");
mCallbacks.kill();
super.onDestroy();
}
private IRemoteService.Stub mBinder = new IRemoteService.Stub() {
@Override
public void registerCallback(IServiceCallback cb) throws RemoteException {
if (cb != null){
boolean isRegister = mCallbacks.register(cb);
Log.i(TAG, "registerCallback: isRegister: "+ isRegister);
}
}
@Override
public void unregisterCallback(IServiceCallback cb) throws RemoteException {
if (cb != null){
boolean isUnRegister = mCallbacks.unregister(cb);
Log.i(TAG, "registerCallback: isUnRegister: "+ isUnRegister);
}
}
};
public static void notifyHybridWork(){
if (mCallbacks == null){
Log.i(TAG, "sendMsgToClient: mCallbacks == null");
return;
}
int n = mCallbacks.beginBroadcast();
for (int i=0; i<n; i++){
try {
mCallbacks.getBroadcastItem(i).valueChanged(true);
} catch (RemoteException e) {
Log.e(TAG, "sendMsgToClient: appear remoteException ");
}
}
mCallbacks.finishBroadcast();
}
// 客户端绑定服务管理类
public class AidlClientManager {
private static final String TAG = "AidlClientManager";
private static IRemoteService mRemoteService;
private static boolean mIsServiceConnected = false;
private static boolean mIsChange= false;
private static final String SERVICE_PACKAGER = "com.zlw.watchface";
private static final String SERVICE_FULL_CLASS_NEME = "com.zlw.aidl.RemoteService";
public static void bindService(Context context){
Intent bindServiceIntent = new Intent();
bindServiceIntent.setComponent(new ComponentName(SERVICE_PACKAGER, SERVICE_FULL_CLASS_NEME));
context.bindService(bindServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG, "bindService()");
}
private static ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected() componentName: " + name);
mRemoteService = IRemoteService.Stub.asInterface(service);
if (mRemoteService != null){
mIsServiceConnected = true;
try {
mRemoteService.registerCallback(mCallback);
} catch (RemoteException e) {
Log.i(TAG, "onServiceConnected: appear remoteException");
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected() componentName: " + name);
if (mRemoteService != null){
try {
mRemoteService.unregisterCallback(mCallback);
} catch (RemoteException e) {
Log.i(TAG, "onServiceDisconnected: appear remoteException");
}
mRemoteService = null;
}
mIsServiceConnected = false;
}
};
private static IServiceCallback mCallback = new IServiceCallback.Stub() {
@Override
public void valueChanged(boolean value) throws RemoteException {
mIsChange= value;
Log.i(TAG, "valueChanged value: " + value);
}
};
public static void unBindService(Context context){
context.unbindService(mServiceConnection);
}
public static boolean isHybrid(){
return mIsChange;
}
}