Service实例-播放mp3音乐
16lz
2021-12-04
1.在main.xml中加入如下四个按钮
2.在res目录下新建raw目录,并拷入mp3文件,我的是www.mp3
创建如下Service
3.在AndroidManifest.xml文件中注册Service
4.Activity中的代码
5.从这个例子中我们可以体会到Context.startService()方法和Context.bindService()方法的区别
在模拟器中点击开启音乐播放服务后,播放音乐,此时点击右侧的回退按钮,音乐仍然播放。但是通过绑定音乐播放服务启动的音乐,在点击回退按钮后,音乐也停止了播放,说明通过这种方式启动的服务是与Context绑定的。
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button1" android:text="开启音乐播放服务"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button2" android:text="停止音乐播放服务"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button3" android:text="绑定音乐播放服务"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button4" android:text="解绑定音乐播放服务"/>
2.在res目录下新建raw目录,并拷入mp3文件,我的是www.mp3
创建如下Service
public class HelloService extends Service {MediaPlayer mp;//定义音乐播放器变量private final static String TAG = "HelloService";@Override//绑定服务 通过 bindService 方法启动服务时调用public IBinder onBind(Intent intent) {Toast.makeText(this, "onBind(Intent intent)",Toast.LENGTH_SHORT).show();mp.start();return null;}@Override//解绑定服务 通过 unbindService 方法解绑定服务时调用public boolean onUnbind(Intent intent) {Toast.makeText(this, "onUnbind(Intent intent)",Toast.LENGTH_SHORT).show();mp.stop();return false;}@Override//创建服务 该服务不存在需要被创建时被调用,不管startService()还是bindService()都会在启动时调用该方法 public void onCreate() {Toast.makeText(this, "onCreate()",Toast.LENGTH_SHORT).show();//创建一个音乐播放器对象mp = MediaPlayer.create(this.getApplicationContext(), R.raw.www); //设置可以重复播放mp.setLooping(true);}@Override //停止服务服务 通过 onDestroy 方法停止服务时调用public void onDestroy() {Toast.makeText(this, "onDestroy()",Toast.LENGTH_SHORT).show();mp.stop();}@Override//开始服务 通过 onStart 方法开始服务时调用public void onStart(Intent intent, int startId) {Toast.makeText(this, "onStart",Toast.LENGTH_SHORT);mp.start();}}
3.在AndroidManifest.xml文件中注册Service
<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".MainActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity> <!-- 注册Service --><service android:name=".service.HelloService"></service></application>
4.Activity中的代码
public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取页面组件Button button1 = (Button) this.findViewById(R.id.button1);Button button2 = (Button) this.findViewById(R.id.button2);Button button3 = (Button) this.findViewById(R.id.button3);Button button4 = (Button) this.findViewById(R.id.button4);// 定义服务链接对象 -要启动哪个服务final ServiceConnection sc = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {Toast.makeText(MainActivity.this,"ServiceConnection onServiceDisconnected",Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Toast.makeText(MainActivity.this,"ServiceConnection onServiceConnected",Toast.LENGTH_SHORT).show();}};// 定义监听事件OnClickListener ocl = new OnClickListener() {//在意图中指定服务Intent intent = new Intent(MainActivity.this, HelloService.class);@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:startService(intent);break;case R.id.button2:stopService(intent);break;case R.id.button3:// 绑定启动要指定服务链接对象,绑定的操作选项bindService(intent, sc, Context.BIND_AUTO_CREATE);break;case R.id.button4:unbindService(sc);break;}}};//为按钮加点击事件button1.setOnClickListener(ocl);button2.setOnClickListener(ocl);button3.setOnClickListener(ocl);button4.setOnClickListener(ocl);}}
5.从这个例子中我们可以体会到Context.startService()方法和Context.bindService()方法的区别
在模拟器中点击开启音乐播放服务后,播放音乐,此时点击右侧的回退按钮,音乐仍然播放。但是通过绑定音乐播放服务启动的音乐,在点击回退按钮后,音乐也停止了播放,说明通过这种方式启动的服务是与Context绑定的。
更多相关文章
- 【翻译】(8-补丁1)Android接口定义语言(AIDL)
- android 开发上传图片遇到返回 FileNotFoundException
- Android(安卓)Button 控件绑定单击事件
- Service实例-播放mp3音乐
- Android(安卓)- AutoCompleteTextView
- android 蓝牙各种UUID
- android service
- Android多进程介绍
- Android推送通知指南