天天看点

AIDL实现跨进程通信

前言:这里以一个app的进程调用另一个app进程的service举例,较为粗略的说明aidl的用法,详细部分放后面补充

一、在服务端中

1.1,首先在服务端进程中,我们需要定义一个aidl接口。

new->aidl->aidl file,然后声明三个抽象方法

void bofangMusic();
void zantingMusic();
void tingzhiMusic();
           

1.2,然后再服务端进程中,我们需要定义一个bindService继承Service,并定义一个AIDL的Stub并实现其抽象方法

public class MyService extends Service{
    MediaPlayer mediaPlayer;
    IBinder myBinder=new MyBinder();

    IMyAidlInterface.Stub stub=new IMyAidlInterface.Stub() {
        @Override
        public void bofangMusic() throws RemoteException {
            if (mediaPlayer==null){
                mediaPlayer=MediaPlayer.create(MyService.this,R.raw.hckz);
                mediaPlayer.setLooping(false);
            }
            if (mediaPlayer!=null&&!mediaPlayer.isPlaying()){
                mediaPlayer.start();
            }
        }

        @Override
        public void zantingMusic() throws RemoteException {
            if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                mediaPlayer.pause();
            }
        }

        @Override
        public void tingzhiMusic() throws RemoteException {
            if (mediaPlayer!=null){
                mediaPlayer.stop();
            }
        }
    };
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return stub;
    }
    public class MyBinder extends Binder {
        public MyService getService(){
            return MyService.this;
        }
    }
}
           

1.3,最后需要在Manifest中配置service并设置action的name属性,用于后面的调用

<service android:name=".MyService"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.zdsoft.service"></action>
            </intent-filter>
        </service>
           

二、客户端

2.1,同样的我们需要新建一个aidl类

这里需要注意的是,要保证与之前的包名一致,否则会报安全错误。可手动建package,并将文件移动过去并修改最上面的包名信息,然后补上同服务端一样的三个抽象方法

2.2,然后我们需要在初始化后通过aidl进行service的绑定工作

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    IMyAidlInterface iMyAidllnterface;
    Button bt_kaishi,bt_zanting,bt_tingzhi;
    private ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidllnterface=IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iMyAidllnterface=null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_kaishi= (Button) findViewById(R.id.bt_kaishi);
        bt_zanting= (Button) findViewById(R.id.bt_zanting);
        bt_tingzhi= (Button) findViewById(R.id.bt_tingzhi);
        bt_kaishi.setOnClickListener(this);
        bt_zanting.setOnClickListener(this);
        bt_tingzhi.setOnClickListener(this);
        conn();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_kaishi:
                try {
                    iMyAidllnterface.bofangMusic();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_zanting:
                try {
                    iMyAidllnterface.zantingMusic();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_tingzhi:
                try {
                    iMyAidllnterface.tingzhiMusic();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
    private void conn(){
        Intent intent=new Intent();
        intent.setAction("com.xinguang.service");
        intent.setPackage("com.xinguang.aidlservice");
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }
}
           

最后我们测试的时候需要先开启服务端进程,然后客户端便可调用

点击这里下载demo

继续阅读