使用AIDL方式启动Service 实现跨进程通信

来源:互联网 发布:golang http 断点续传 编辑:程序博客网 时间:2024/04/29 09:57

创建aidl文件
注意:aidl文件中可以引用其它aidl文件中定义的接口,但是不能够引用java类文件中
定义的接口。创建时在Project模式在,选中src/main文件夹,new->adil文件新建的aidl
文件需要make才能编译生

// MusicAidlInterface.aidlpackage com.zdsoft.aidlservice1228;// Declare any non-default types here with import statementsinterface MusicAidlInterface {     void play();     void pause();     void stop();}
Service中实现接口
package com.zdsoft.aidlservice1228;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;import android.os.RemoteException;import java.io.IOException;public class MusicService extends Service {    private MediaPlayer mediaPlayer;    public MusicAidlInterface.Stub binder = new MusicAidlInterface.Stub() {        @Override        public void play() throws RemoteException {            if (mediaPlayer == null) {                mediaPlayer = MediaPlayer.create(MusicService.this, R.raw.zoutianyan);            }            if (mediaPlayer != null) {                mediaPlayer.start();            }        }        @Override        public void pause() throws RemoteException {            if (mediaPlayer != null && mediaPlayer.isPlaying()) {                mediaPlayer.pause();            }        }        @Override        public void stop() throws RemoteException {            if (mediaPlayer != null && mediaPlayer.isPlaying()) {                mediaPlayer.stop();                try {                    mediaPlayer.prepare();                    mediaPlayer.seekTo(0);                } catch (IOException e) {                    e.printStackTrace();                }            }        }    };    @Override    public IBinder onBind(Intent intent) {        return binder;    }    @Override    public void onDestroy() {        super.onDestroy();        if (mediaPlayer != null) {            mediaPlayer.stop();            mediaPlayer.release();        }    }}
AndroidManifest.xml中注册service并配置intent-filter,
exported="true"允许被其他程序调用
<service            android:name=".MusicService"            android:enabled="true"            android:exported="true">            <intent-filter >                <action android:name="com.zdsoft.aidlservice.MUSIC_SERVICE"/>            </intent-filter>
编写客户端app调用,把xxx.aidl文件拷贝一份到客户端(注意包路径要一致)

在activity中实现播放音乐功能

package com.zdsoft.aidlclient1228;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import com.zdsoft.aidlservice1228.MusicAidlInterface;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button bt_play, bt_pause, bt_stop, bt_exit, bt_exit_stop;    private MusicAidlInterface musicAidlInterface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        listener();        Intent intent = new Intent();        intent.setAction("com.zdsoft.aidlservice.MUSIC_SERVICE");        intent.setPackage("com.zdsoft.aidlservice1228");        bindService(intent, connection, Service.BIND_AUTO_CREATE);    }    @Override    protected void onDestroy() {        super.onDestroy();        if (connection != null) {            unbindService(connection);        }    }    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            if (musicAidlInterface == null) {                musicAidlInterface = MusicAidlInterface.Stub.asInterface(service);            }        }        @Override        public void onServiceDisconnected(ComponentName name) {            if (musicAidlInterface != null) {                musicAidlInterface = null;            }        }    };    private void initView() {        bt_play = (Button) findViewById(R.id.bt_play);        bt_pause = (Button) findViewById(R.id.bt_pause);        bt_stop = (Button) findViewById(R.id.bt_stop);        bt_exit = (Button) findViewById(R.id.bt_exit);        bt_exit_stop = (Button) findViewById(R.id.bt_exit_stop);    }    private void listener() {        bt_play.setOnClickListener(this);        bt_pause.setOnClickListener(this);        bt_stop.setOnClickListener(this);        bt_exit.setOnClickListener(this);        bt_exit_stop.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.bt_play:                try {                    musicAidlInterface.play();                } catch (RemoteException e) {                    e.printStackTrace();                }                break;            case R.id.bt_pause:                try {                    musicAidlInterface.pause();                } catch (RemoteException e) {                    e.printStackTrace();                }                break;            case R.id.bt_stop:                try {                    musicAidlInterface.stop();                } catch (RemoteException e) {                    e.printStackTrace();                }                break;            case R.id.bt_exit:                finish();                break;            case R.id.bt_exit_stop:                try {                    musicAidlInterface.stop();                } catch (RemoteException e) {                    e.printStackTrace();                }                finish();                break;        }    }}


1 0