利用服務播放音樂的實例,大部分採用SDK上的方式,
MainActivity:
package com.example.androidtest; import com.example.androidtest.myService.LocalBinder; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { protected myService mService;//用於保存sevice所返回的myService對象,以調用Service中所提供的方法 protected boolean mBound;//用於判斷綁定情況 private ServiceConnection mConnection = new ServiceConnection() {//作爲bindService參數,在綁定後將調用 onServiceConnected,並接受到返回的IBinder對象,該對象可用於獲取service所要提供的數據,本例中由於在服務中的LocalBinder添加了getService方法,故可用於獲取myService對象 @Override public void onServiceConnected(ComponentName className, IBinder service) { LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) {//在unbindService後調用,用作清理資源 mBound = false; mService.stopMusic(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent(this, myService.class), mConnection, Context.BIND_AUTO_CREATE);//綁定服務 Button startButton = (Button) findViewById(R.id.button1); Button pauseButton = (Button) findViewById(R.id.button2); Button stopButton = (Button) findViewById(R.id.button3); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mService.playMusic(); // TODO Auto-generated method stub } }); pauseButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mService.pauseMusic(); // TODO Auto-generated method stub } }); stopButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mService.stopMusic(); } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (mBound == true) { unbindService(mConnection);//解除綁定 } } }
myService:
package com.example.androidtest; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class myService extends Service { private LocalBinder localBinder=new LocalBinder();; private MediaPlayer mediaPlayer;//創建一個以raw/music爲播放文件的MediaPlayer對象,可用於播放音樂 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub mediaPlayer=MediaPlayer.create(myService.this,R.raw.music); return localBinder; } //創建內部類作爲onBind返回值,用於給客戶端獲取服務端的載體 public class LocalBinder extends Binder { myService getService() {//直接返回myService對象,個人認爲此種方法比較簡單,也可在此創建其他需要返回給客戶端的方法,用於通信 return myService.this; } } //客戶端可調用方法: public void playMusic() { mediaPlayer.start(); } public void pauseMusic() { mediaPlayer.pause(); } public void stopMusic(){ mediaPlayer.stop(); } }