Android进程间的通信--ALDL(不同应用通信)

一,简介

ALDL:全称:Android Interface definition language(安卓接口定义语言)
			作用:进程间的通信接口 进程间的数据共享

二,实现进程间的服务启动及绑定

这里只是实现了不同应用之间的服务绑定和启动

1.在被远程应用中添加services别名

<service
        android:name=".MyServices"
        android:enabled="true"
        android:exported="true">
        <!--创建别名,在另一个应用使用-->
        <intent-filter>
            <action android:name="com.zc.services"/>
        </intent-filter>
    </service>

2.在远程应用中注册

 @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.start:
                Intent intent = new Intent();
                intent.setAction("com.zc.services");
                //被远程应用的包名
                intent.setPackage("com.example.mr.servicedome");
                startService(intent);
                break;
            case R.id.stop:
                Intent intent2 = new Intent();
                intent2.setAction("com.zc.services");
                intent2.setPackage("com.example.mr.servicedome");
                stopService(intent2);
                break;
            case R.id.bind:

                Intent intent3 = new Intent();
                intent3.setAction("com.zc.services");
                intent3.setPackage("com.example.mr.servicedome");
                bindService(intent3,serviceConnection,BIND_AUTO_CREATE);

                break;
            case R.id.unbind:
                unbindService(serviceConnection);
                break;
        }

三,实现进程间的数据共享

1.在被远程应用中创建ALDL文件 创建并自定义进度方法 还要Build生成Java文件

在这里插入图片描述
在这里插入图片描述
在被远程应用中修改

  @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this,"绑定了服务",Toast.LENGTH_SHORT).show();
        Log.e("tt","绑定了服务");
//        throw new UnsupportedOperationException("Not yet implemented");
        //同应用之间的进度传递
//        return new Mbinder();
        //不同应用之间的进度传递 在这里
        return new IMyAidlInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }
            //这个是自定义的方法
            @Override
            public void showi() throws RemoteException {
                Log.e("tt","当前进度" + i);
            }
        };
    }
    private ServiceConnection serviceConnection = new ServiceConnection() {
        //当客户端和服务正常连接时 执行的绑定操作会被调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //3.获取自定义service中返回来了的service中Mbinder获取进度
//            MyServices.Mbinder mbinder = (MyServices.Mbinder)service;
//            Log.e("tt","" + mbinder.geii());
//------------不同应用的进度------------------------------------------------------------------------------------
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                iMyAidlInterface.showi();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        //当客户端和服务连接丢失 会被调用
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

在远程应用中操作 复制刚在被远程应用中的aldl文件到远程应用中 注意 要整个文件夹都复制过去 位置相同
在这里插入图片描述
就可以在ServiceConnection 中使用了

  private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface iMyAidlInterface =IMyAidlInterface.Stub.asInterface(service);
            try {
                iMyAidlInterface.showi();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43609490/article/details/87978407