代码展示AIDL进程间通信 服务端向客户端发送消息

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;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值