标签:
Android允许我们使用Service组件来完成某些后台任务,但这些任务的允许不会影响到用户其他的交互。
一、Activity类
1 package demo.camera; 2 import android.app.Activity; 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.view.View; 10 /** 11 * 演示Activity如何利用Service来完成后台Audio的播放功能 12 * 同时如何将Service和Activity进行绑定 13 * @author Administrator 14 * 15 */ 16 public class BackgroundAudioDemo extends Activity { 17 18 private AudioService audioService; 19 20 //使用ServiceConnection来监听Service状态的变化 21 private ServiceConnection conn = new ServiceConnection() { 22 23 @Override 24 public void onServiceDisconnected(ComponentName name) { 25 // TODO Auto-generated method stub 26 audioService = null; 27 } 28 29 @Override 30 public void onServiceConnected(ComponentName name, IBinder binder) { 31 //这里我们实例化audioService,通过binder来实现 32 audioService = ((AudioService.AudioBinder)binder).getService(); 33 34 } 35 }; 36 37 public void onCreate(Bundle savedInstanceState){ 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.back_audio); 40 } 41 42 43 public void onClick(View v){ 44 int id = v.getId(); 45 Intent intent = new Intent(); 46 intent.setClass(this, AudioService.class); 47 if(id == R.id.btn_start){ 48 //启动Service,然后绑定该Service,这样我们可以在同时销毁该Activity,看看歌曲是否还在播放 49 startService(intent); 50 bindService(intent, conn, Context.BIND_AUTO_CREATE); 51 finish(); 52 }else if(id == R.id.btn_end){ 53 //结束Service 54 unbindService(conn); 55 stopService(intent); 56 finish(); 57 }else if(id == R.id.btn_fun){ 58 audioService.haveFun(); 59 } 60 } 61 }
2、Service类
1 package demo.camera; 2 import android.app.Service; 3 import android.content.Intent; 4 import android.media.MediaPlayer; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.widget.MediaController.MediaPlayerControl; 8 /** 9 * 为了可以使得在后台播放音乐,我们需要Service 10 * Service就是用来在后台完成一些不需要和用户交互的动作 11 * @author Administrator 12 * 13 */ 14 public class AudioService extends Service implements MediaPlayer.OnCompletionListener{ 15 16 MediaPlayer player; 17 18 private final IBinder binder = new AudioBinder(); 19 @Override 20 public IBinder onBind(Intent arg0) { 21 // TODO Auto-generated method stub 22 return binder; 23 } 24 /** 25 * 当Audio播放完的时候触发该动作 26 */ 27 @Override 28 public void onCompletion(MediaPlayer player) { 29 // TODO Auto-generated method stub 30 stopSelf();//结束了,则结束Service 31 } 32 33 //在这里我们需要实例化MediaPlayer对象 34 public void onCreate(){ 35 super.onCreate(); 36 //我们从raw文件夹中获取一个应用自带的mp3文件 37 player = MediaPlayer.create(this, R.raw.tt); 38 player.setOnCompletionListener(this); 39 } 40 41 /** 42 * 该方法在SDK2.0才开始有的,替代原来的onStart方法 43 */ 44 public int onStartCommand(Intent intent, int flags, int startId){ 45 if(!player.isPlaying()){ 46 player.start(); 47 } 48 return START_STICKY; 49 } 50 51 public void onDestroy(){ 52 //super.onDestroy(); 53 if(player.isPlaying()){ 54 player.stop(); 55 } 56 player.release(); 57 } 58 59 //为了和Activity交互,我们需要定义一个Binder对象 60 class AudioBinder extends Binder{ 61 62 //返回Service对象 63 AudioService getService(){ 64 return AudioService.this; 65 } 66 } 67 68 //后退播放进度 69 public void haveFun(){ 70 if(player.isPlaying() && player.getCurrentPosition()>2500){ 71 player.seekTo(player.getCurrentPosition()-2500); 72 } 73 } 74 }
3、在清单文件AndroidManifest.xml中配置Service
<service
android:name=".AudioService" />
标签:
原文地址:http://www.cnblogs.com/swq934063979/p/4617348.html