Android进程间通信之AIDL

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37292229/article/details/71080560

IPC是Inter-Process Communication的缩写,含义为进程间通信或跨进程通信,是指两个进程之间进行数据交互的过程。这里介绍AIDL来进行进程间通信的流程,分为服务端和客户端两个方面。
一:服务端
1.1:创建一个Service来监听客户端的连接请求;
1.2:创建一个AIDL文件,将暴露给客户端调用的接口在这个AIDL文件声明;
1.3:最后,在Service中实现这个AIDL接口;
二:客户端
2.1:绑定服务端的Service;
2.2:将服务端返回的Binder对象转成AIDL接口所属的类型;
2.3:然后调用AIDL中的方法
具体实现
三:服务端的具体实现
3.1:新建aidl文件
这里写图片描述

会自动创建aidl包名和文件,在aidl文件中编写我们暴露给客户端的接口;
接下来编写我们要暴露给客户端调用的接口代码:

// IUserAidl.aidl
package com.zx.mvvmdemo;
import com.zx.mvvmdemo.User;

interface IUserAidl {
    void addUser(in User user);  //添加用户
    List<User> getUserList();    //获取用户list
}

3.2:创建一个Service来监听客户端的连接请求,并实现上面aidl的接口

public class AidlDemoService extends Service {
    public AidlDemoService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /**
     * CopyOnWriteArrayList支持并发的读写
     */
    private CopyOnWriteArrayList<User> mUserList = new CopyOnWriteArrayList();
    private Binder mBinder = new IUserAidl.Stub() {
        @Override
        public void addUser(User user) throws RemoteException {
            mUserList.add(user);
        }

        @Override
        public List<User> getUserList() throws RemoteException {
            return mUserList;
        }
    };
}

清单文件中注册

   <service
            android:name=".service.AidlDemoService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.nld.appstore.AidlDemoService"/>
            </intent-filter>
        </service>

运行app,开启服务

二:建立Client
1.把Server的aidl文件,导入到client工程中。

2.绑定服务端的Service,将服务端返回的Binder对象转成AIDL接口所属的类型,然后调用AIDL中的方法


public class MainActivity extends AppCompatActivity {
    private IRemoteService remoteService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mBtn = (Button) findViewById(R.id.btn_start);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //2.1绑定服务端的service
                Intent intent = new Intent();
                intent.setAction("com.nld.appstore.AidlDemoService");
                 intent.setPackage("com.nld.appstore");//进程名
                bindService(intent, conn, BIND_AUTO_CREATE);
            }
        });
    }

    ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            remoteService = IRemoteService.Stub.asInterface(service);
            //调用aidl中的接口
            remoteService.addUser(user);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}

客户端,把服务端aidl文件完整的复制到客户端下面。

package com.newland.app.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import aidl.com.newland.pospp.mtms.core.AppListAidlInterface;
import aidl.com.newland.pospp.mtms.core.OnQueryInfoListener;

import static android.content.Context.BIND_AUTO_CREATE;

/**
 * author labo
 * date on 2018/6/7.
 * desc
 */

public class GetMtmsAppListUtils {
    private void go2Service(Context context) {
        Intent intent = new Intent();
        intent.setAction("com.nld.appstore.QueryAppValidPeriodService");
        intent.setPackage("com.nld.appstore");
      context.  bindService(intent, conn, BIND_AUTO_CREATE);
    }
  ServiceConnection conn=  new ServiceConnection(){
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
          AppListAidlInterface aidl = AppListAidlInterface.Stub.asInterface(service);
          try {
              aidl.getAppList(new OnQueryInfoListener.Stub() {
                  @Override
                  public void onSuccess(String code, String appList) throws RemoteException {

                  }
              });
          } catch (RemoteException e) {
              e.printStackTrace();
          }
      }

      @Override
      public void onServiceDisconnected(ComponentName name) {

      }
  };

}

猜你喜欢

转载自blog.csdn.net/weixin_37292229/article/details/71080560