使用bindService方式启动Service
16lz
2021-01-26
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(安卓)即时音效SoundPool
- 播放记录的SQLite数据库实现
- Android_播放器的进度条
- android使用opengl es2.0播放视频
- Android(安卓)Media Recorder录音播放源代码
- Android之setOnClickListener()
- Android中的Parcelable接口和Serializable使用方法和差别
- Android原生方法和Web JS互相调用
- 【短信】短信模块的多平台适配