使用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

            
                
            
        


你可能感兴趣的:(Android,案例)