/********************************************************************************************
* author:conowen@大鐘
* E-mail:conowen@hotmail.com
* http://blog.csdn.net/conowen
* 註:本文為原創,僅作為學習交流使用,轉載請標明作者及出處。
********************************************************************************************/
1、首先service如windows下面的服務是差不多的,沒有使用者介面,只是在後台工作。
2、看一下service的繼承關係
3、service本身不能自己啟動,要調用其他方法才能啟動service。有如下兩種方法
a、Context.startService()
使用這個方法時,service與啟動者是沒有關係的,只是單純的啟動service,若啟動者退出了,service依舊在後台運行。
(如音樂播放,即使退出了程式,音樂還在播放著)
b、Context.bindService()
使用此方法時,顧名思義,就是把啟動者與service綁定在一起了,啟動者退出,service也跟著停止。
4、service的生命週期
a、採用startService()方法啟動的服務,只能調用stopService()方法結束服務,服務結束時會自動調用onDestroy()方法。
b、採用bindService()方法啟動的服務,可以調用unbindService()方法來解除綁定,調用該方法時,也會同時導致系統自動調用服務的onUnbind()和onDestroy()方法.
//Context.startService方式的生命週期:啟動時,調用startService方法 –>onCreate() –> onStart()停止時,調用stopService 方法 –> onDestroy()//Context.bindService方式的生命週期:綁定時,調用bindService方法 ->onCreate() –> onBind()解除綁定定時,調用unbindService方法 –>onUnbind() –> onDestory(
Service的生命週期方法比Activity少,只有onCreate, onStart, onDestroy 。
5、簡單音樂播放例子
MP3檔案在res目錄下的raw目錄裡面,拖動mp3檔案進去即可。(raw為自己建立)
檔案格式為mp3和ogg,檔案名稱為0~9或者a~z,位元速率最好低於或等於320kps
//第一個Activitypackage conowen.szu.Service;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;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class ServiceActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button play = (Button) findViewById(R.id.play);Button stop = (Button) findViewById(R.id.stop);Button bind = (Button) findViewById(R.id.bind);Button unbind = (Button) findViewById(R.id.unbind);final Intent intent = new Intent(ServiceActivity.this,MusicService.class);// 定義intent為final,全域變數,供下面兩個匿名內部類(onclicklisenter)使用play.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubstartService(intent);// 開始播放,通過前面定義好的intent傳遞}});stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubstopService(intent);// 停止播放}});/*******************************以下為bindService方式啟動Service*****************************************/final ServiceConnection conn=new ServiceConnection() {//全域變數final ,供下面兩個內部類使用@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubToast.makeText(ServiceActivity.this, "綁定成功", Toast.LENGTH_LONG).show();}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubToast.makeText(ServiceActivity.this, "解除綁定成功", Toast.LENGTH_LONG).show();}};//當採用bindService啟動Service,Service和執行bindService所在的Activity綁定成功以後會調用ServiceConnection;//同理,當的Service和使用該Service的用戶端取消綁定以後會調用onServiceDisconnected //ServiceConnection(conn)也為下面的bindService和unbindService的參數bind.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbindService(intent, conn, Context.BIND_AUTO_CREATE);}});unbind.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubunbindService(conn);}});}}
第二個class為service的處理方法,通過複寫各種service方法實現不同的功能。
//第二個classpackage conowen.szu.Service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class MusicService extends Service {MediaPlayer player;//建立播放器@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();player = MediaPlayer.create(this, R.raw.lt26);player.setLooping(true);//兩種方式的第一個生命週期都是onCreate()}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);player.start();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubplayer.start();return null;}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubplayer.stop();return super.onUnbind(intent);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();player.stop();}}效果圖:
6、
註:第二個java檔案
public class MusicService extends Service {繼承的是Service,則要在AndroidManifest裡面開啟Service
<service android:label="player" android:name=".MusicService" > </service>
若第一個java檔案繼承的是Activity,則AndroidManifest裡面為
<activity android:label="player" android:name=".MusicService" > </activity>
/********************************************************/
對於每多一次點擊“播放”按鈕時,音樂就多播放一個。(都是同一個音樂,但是播放起步時間不同)
可以把player.start()方法置於onCreate裡面,這樣就算多次點擊“播放”按鈕,也沒事。因為Service的onCreate的方法只會被調用一次。
如下代碼所示(只有“播放”與“停止功能”)並沒有加入綁定與解除綁定
onStart方法不用寫出
package conowen.szu.Service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class MusicService extends Service {MediaPlayer player;@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();player = MediaPlayer.create(this, R.raw.lt26);player.start();}// player.start()在onCreate方法裡面時,多次點擊play按鈕,一樣正常播放,也就是說音樂不會再開多一個。 //若play.start()放在onStart方面裡面,沒多一次點擊play按鈕,就會多播放一次,同樣的音樂同時播放(起步不同)@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();player.stop();}}