Activity四大组件之Service 方式二BindService

res文件夹下建raw文件夹放入要播放的音乐

同样先注册

AndroidManifest.xml类

<service
            android:name="包名.类名"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.bindservice.action.MybindService" />  //进行绑定相当于标识符
            </intent-filter>
        </service>

BindService类


public class bindService extends Service{
	
	/** 2、Binder接口的对象*/
	private final IBinder binder=new MyBinder();
	private MediaPlayer player;

	/** 1、定义内部类继承Binder,实现IBinder接口,并提供方法返回实例*/
	public class MyBinder extends Binder{
		public bindService getService(){
			return bindService.this;
		}
	}
	
	/** 3、实现Service的抽象方法onBind,并返回一个实现IBinder接口的对象*/
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return binder;
	}


	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(player!=null){
			player.stop();
			player.release();
		}
	}

	/** 4、提供媒体播放的方法*/
	public void start(){
		if(player==null){
			player=MediaPlayer.create(this, com.bindservice.action.R.raw.let_it_out);
		}
		if(player !=null && !player.isPlaying()){
			player.start();
		}
	}
	
	//暂停
	public void pause(){
		if(player!=null && player.isPlaying()){
			player.pause();
		}
	}
	
	//停止
	public void stop(){
		if(player!=null && player.isPlaying()){
			player.stop();
			try{
				// 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数
				player.prepare();
			}catch(IOException io){
				io.printStackTrace();
			}
		}
	}
}

MainActivity类

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_play, btn_suspend, btn_stop, btn_quit;
	private String BindCode = "com.bindservice.action.MybindService";  //与AndroidManifest.xml类类中定义的绑定符相同
	private bindService bservice;

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

	private void init() {
		btn_play = (Button) findViewById(R.id.btn_play);
		btn_suspend = (Button) findViewById(R.id.btn_suspend);
		btn_stop = (Button) findViewById(R.id.btn_stop);
		btn_quit = (Button) findViewById(R.id.btn_quit);

		btn_quit.setOnClickListener(this);
		btn_stop.setOnClickListener(this);
		btn_suspend.setOnClickListener(this);
		btn_play.setOnClickListener(this);
	}

	/**
	 * 5、Activity里定义ServiceConnection接口对象并实现ServiceConnection接口
	 * 重写onServiceConnected方法进行Service销毁 重写onServiceDisconnected方法进行Service连接
	 * */
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub
			bservice = null;
		}

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			// TODO Auto-generated method stub
			bservice = ((bindService.MyBinder) (arg1)).getService();
			if (bservice != null) {
				bservice.pause();
			}
		}
	};

	/** 6、Activity的onCreate方法里绑定musicService */
	private void conntion() {
		// 6-1、使用服务绑定标示实例化Intent对象
		Intent intent = new Intent(BindCode);
		// 6-2、使用bindService(Intent对象,ServiceConnection接口对象,标志位)绑定bindService。
		/**
		 * 注:标志位Context.BIND_AUTO_CREATE,即数字1 说明:表示收到绑定请求的时候,如果服务尚未创建,则即刻创建,
		 * 在系统内存不足需要先摧毁优先级组件来释放内存, 且只有驻留该服务的进程成为被摧毁对象时,服务才被摧毁
		 */
		bindService(intent, conn, 1);
	}

	/** 7、使用bservice方法控制音乐*/
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btn_play:
			bservice.start();
			break;

		case R.id.btn_suspend:
			bservice.pause();
			break;

		case R.id.btn_stop:
			bservice.stop();
			break;

		case R.id.btn_quit:
			finish();
			break;

		default:
			break;
		}
	}
	
	/**退出时解除绑定*/
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(conn!=null){
			unbindService(conn);
		}
	}

}

但是,停止功能与暂停功能相同



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值