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

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

// MusicAidlInterface.aidl
package com.zdsoft.aidlservice1228;

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

interface 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值