使用bindService方式启动Service

service
package com.zdsoft.bindservice1227;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import java.io.IOException;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;
    //2、实例化IBinder接口对象
    private IBinder binder = new MyBinder();

    /**
     * 1、定义内部类继承Binder
     */
    public class MyBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        //3、返回binder
        return binder;
    }

    @Override
    public void onDestroy() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }

    }

    /**
     * 播放
     */
    public void play() {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.zoutianyan);
        }
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    /**
     * 停止
     */
    public void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            try {
                //准备下次播放
                mediaPlayer.prepare();
                //设置播放位置
                mediaPlayer.seekTo(0);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

activity

package com.zdsoft.bindservice1227;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_play, bt_pause, bt_stop, bt_exit, bt_exit_stop;
    private MusicService musicService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        listener();
        //绑定:
        Intent intent = new Intent();
        intent.setAction("com.zdsoft.bindservice.MUSIC_SERVICE");
        intent.setPackage("com.zdsoft.bindservice1227");
        bindService(intent, connection, Service.BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑
        unbindService(connection);
    }

    /**
     * 定义ServiceConnection接口对象并实现接口
     */
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (musicService == null) {
                musicService = ((MusicService.MyBinder) service).getService();
                musicService.play();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = 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:
                musicService.play();
                break;
            case R.id.bt_pause:
                musicService.pause();
                break;
            case R.id.bt_stop:
                musicService.stop();
                break;
            case R.id.bt_exit:
                finish();
                break;
            case R.id.bt_exit_stop:
                musicService.stop();
                finish();
                break;
        }

    }
}
AndroidManifest.xml中注册service并配置intent-filter
<service
            android:name=".MusicService"
            android:enabled="true"
            android:exported="true">
            <intent-filter >
                <action android:name="com.zdsoft.bindservice.MUSIC_SERVICE"></action>
            </intent-filter>
        </service>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值