Android同意我們使用Service組件來完畢后台任務。這些任務的同意不會影響到用戶其它的交互。
1、Activity類
- package demo.camera;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.View;
- /**
- * 演示Activity怎樣利用Service來完畢后台Audio的播放功能
- * 同一時候怎樣將Service和Activity進行綁定
- * @author Administrator
- *
- */
- public class BackgroundAudioDemo extends Activity {
- private AudioService audioService;
- //使用ServiceConnection來監聽Service狀態的變化
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- audioService = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder binder) {
- //這里我們實例化audioService,通過binder來實現
- audioService = ((AudioService.AudioBinder)binder).getService();
- }
- };
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.back_audio);
- }
- public void onClick(View v){
- int id = v.getId();
- Intent intent = new Intent();
- intent.setClass(this, AudioService.class);
- if(id == R.id.btn_start){
- //啟動Service。然后綁定該Service,這樣我們能夠在同一時候銷毀該Activity,看看歌曲是否還在播放
- startService(intent);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- finish();
- }else if(id == R.id.btn_end){
- //結束Service
- unbindService(conn);
- stopService(intent);
- finish();
- }else if(id == R.id.btn_fun){
- audioService.haveFun();
- }
- }
- }
2、Service類
- package demo.camera;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- import android.widget.MediaController.MediaPlayerControl;
- /**
- * 為了能夠使得在后台播放音樂,我們須要Service
- * Service就是用來在后台完畢一些不須要和用戶交互的動作
- * @author Administrator
- *
- */
- public class AudioService extends Service implements MediaPlayer.OnCompletionListener{
- MediaPlayer player;
- private final IBinder binder = new AudioBinder();
- @Override
- public IBinder onBind(Intent arg0) {
- // TODO Auto-generated method stub
- return binder;
- }
- /**
- * 當Audio播放完的時候觸發該動作
- */
- @Override
- public void onCompletion(MediaPlayer player) {
- // TODO Auto-generated method stub
- stopSelf();//結束了。則結束Service
- }
- //在這里我們須要實例化MediaPlayer對象
- public void onCreate(){
- super.onCreate();
- //我們從raw目錄中獲取一個應用自帶的mp3文件
- player = MediaPlayer.create(this, R.raw.tt);
- player.setOnCompletionListener(this);
- }
- /**
- * 該方法在SDK2.0才開始有的。替代原來的onStart方法
- */
- public int onStartCommand(Intent intent, int flags, int startId){
- if(!player.isPlaying()){
- player.start();
- }
- return START_STICKY;
- }
- public void onDestroy(){
- //super.onDestroy();
- if(player.isPlaying()){
- player.stop();
- }
- player.release();
- }
- //為了和Activity交互,我們須要定義一個Binder對象
- class AudioBinder extends Binder{
- //返回Service對象
- AudioService getService(){
- return AudioService.this;
- }
- }
- //后退播放進度
- public void haveFun(){
- if(player.isPlaying() && player.getCurrentPosition()>2500){
- player.seekTo(player.getCurrentPosition()-2500);
- }
- }
- }
3、在清單文件AndroidManifest.xml中配置Service
<service
android:name=".AudioService" />