Android AIDL 简单demo 跨进程访问service

1.定义AIDL接口:需要保证包名一样

在Android Studio中 src目录上右键创建一个AIDL文件 并命名,完成后会再main下自动生成一个aidl目录,该目录的包名和java下的包名是一致的。 

uploading.4e448015.gif转存失败重新上传取消

 

 

2.make Project 然后生成接口

3,哪里需要用拷贝接口过来

 

 

通过AIdl,bind启动远程服务

private boolean bindStepService() {
    Activity activity = getReference();
    if (activity != null) {
        try {
            Intent service = new Intent(activity, ModuleHub.moduleSport().getStepService());
            return activity.bindService(service, mStepConnect, Context.BIND_IMPORTANT);
        } catch (Exception e) {
            return false;
        }
    }
    return false;
}

 

和服务绑定之后,设置监听到服务里面,服务有数据变化,通知过来

private StepCountListener mStepCountListener = new StepCountListener(this);
private IServiceAutoStepInterface mServiceAutoStepInterface;
private ServiceConnection mStepConnect = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mServiceAutoStepInterface = IServiceAutoStepInterface.Stub.asInterface(service);
        try {
            mServiceAutoStepInterface.registerListener(mStepCountListener);
        } catch (Throwable e) {
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        try {
            //mServiceAutoStepInterface android.os.DeadObjectException
            if (!AndroidUtils.haveRunProcess(ShadowApp.context()) && mServiceAutoStepInterface != null)
                mServiceAutoStepInterface.unRegisterListener(mStepCountListener);
            mStepCountListener = null;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        mServiceAutoStepInterface = null;
    }
};

 

Service里面注册监听的过程:

  private List<IStepCountListener> mArrayList = Collections.synchronizedList(new ArrayList<IStepCountListener>());
    private IBinder mBinder = new IServiceAutoStepInterface.Stub() {
        @Override
        public void registerListener(IStepCountListener listener) throws android.os.RemoteException {
            mArrayList.clear();
            if (null != listener) {
                if (-1 == getStepCountListenerIndex(listener)) {
                    mArrayList.add(listener);
                    YDLog.logWannig(kTag, "register listener ,list not found");
                }
                YDLog.logInfo(kTag, "register listener ,cur size :" + mArrayList.size());
            } else {
                throw new android.os.RemoteException();
            }
        }

        @Override
        public void unRegisterListener(IStepCountListener listener) throws android.os.RemoteException {
            int index = getStepCountListenerIndex(listener);
            mArrayList.clear();
//            if (index >= 0 && index < mArrayList.size()) {
//                mArrayList.remove(index);
            YDLog.logInfo(kTag, "unRegister listener ,remove succ");
//            }
            YDLog.logInfo(kTag, "unRegister listener,callback");
        }

        @Override
        public int getCurStepCount() throws android.os.RemoteException {
            return todayStepCount;
        }

        @Override
        public void onStepDetectedCallBack(int step, long st, long et) throws RemoteException {

        }
    };

 

主要是要拿到binder对象,在启动服务之后可以拿到

@Nullable
@Override
public IBinder onBind(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setForegroundService();
    }
    return mBinder;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章