Android Service的使用方法 音乐播放器实例

本文介绍如何在Android中利用Service实现音乐播放器的后台播放功能,避免系统自动关闭,确保音乐播放不间断。

Service翻译成中文是服务,熟悉Windows 系统的同学一定知道很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行,避免被用户误关闭。因为Android在某些情况下会自动关闭非前台显示的Activity,所以如果要让一个功能在后台一直执行,不被Android系统关闭,比如说闹钟、后台播放音乐,就必须使用Service.
之前开发音乐播放器的时候也没用Service,但是却可以后台播放,以为Service没什么用,但是经过一段时间后发现,没用Service的播放器在播放一段时间后会被系统自动关闭,而且就算还在后台播放,过一段时间后打开播放器,再点播放按钮,会出现两种声音,今天用Service写了个Demo,完美解决以上问题。
布局XML,就两个按钮:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button android:id="@+id/start"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="开始"
  11. />
  12. <Button android:id="@+id/stop"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="停止"
  16. />
  17. </LinearLayout>

主程序main.java:

  1. package com.pocketdigi.service;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10.  
  11. public class main extends Activity {
  12. /** Called when the activity is first created. */
  13. Button start,stop;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. findView();
  19. start.setOnClickListener(startlis);
  20. stop.setOnClickListener(stoplis);
  21. }
  22. private OnClickListener startlis=new OnClickListener(){
  23.  
  24. @Override
  25. public void onClick(View v) {
  26. // TODO Auto-generated method stub
  27. startService(new Intent(main.this, Music.class));
  28. //启动服务
  29. }
  30. };
  31. private OnClickListener stoplis=new OnClickListener(){
  32.  
  33. @Override
  34. public void onClick(View v) {
  35. // TODO Auto-generated method stub
  36. stopService(new Intent(main.this,Music.class));
  37. //停止服务
  38. }
  39. };
  40. public void findView(){
  41. start=(Button)findViewById(R.id.start);
  42. stop=(Button)findViewById(R.id.stop);
  43. }
  44. }

服务 Music.java:

  1. package com.pocketdigi.service;
  2.  
  3.  
  4. import android.app.Service;
  5. import android.content.Intent;
  6. import android.media.MediaPlayer;
  7. import android.os.IBinder;
  8.  
  9. public class Music extends Service {
  10. private MediaPlayer mp;
  11. @Override
  12. public IBinder onBind(Intent intent) {
  13. // TODO Auto-generated method stub
  14. return null;
  15. }
  16. @Override
  17. public void onCreate() {
  18. super.onCreate();
  19. mp=MediaPlayer.create(this,R.raw.xrx);
  20. }
  21. @Override
  22. public void onStart(Intent intent, int startId) {
  23. super.onStart(intent, startId);
  24. mp.start();
  25. }
  26. @Override
  27. public void onDestroy() {
  28. super.onDestroy();
  29. mp.stop();
  30. }
  31.  
  32. }

服务还需要在AndroidManifest.xml注册后才能使用:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.pocketdigi.service"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".main"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <service android:enabled="true" android:name=".Music" />
  15. </application>
  16. </manifest>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值