android Aidl 实现进程间通讯

1​,android 新建aidl 文件 删除void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);这个方法没用到 还需要实现。

android Aidl 实现进程间通讯_第1张图片

2,添加自己需要的方法例如

package com.cglt.cutticket;

// Declare any non-default types here with import statements

interface CgltechAidl {
     int getApkVersionCode();
}

3,定义Service,定内部类CgltehcBinder 实现Aidl接口,并且onBind返回实现类

public class AidlService extends Service {
    class CgltehcBinder extends CgltechAidl.Stub {
        public CgltehcBinder() {
            Log.e("biner", "biner");
        }

        @Override
        public int getApkVersionCode() throws RemoteException {
            return AppInfo.apkVerisonCode;
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new CgltehcBinder();
    }
}

4,配置Service,action为CgltechAidlAction


    
        
    

5,另一个apk绑定这个service

Intent intent = new Intent();
intent.setAction("CgltechAidlAction");//Service的action
intent.setPackage("com.cglt.cutticket");//Service 所在包名
bindService(intent, new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.e("fff","ok");
        cgltechAidl = CgltechAidl.Stub.asInterface(service);
        try {
            Log.e("fff",cgltechAidl.getApkVersionCode()+"/");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.e("fff","no");
    }
}, BIND_AUTO_CREATE);

你可能感兴趣的:(Android)