Android系統也提供了一種稱爲「Service」的組件一般在後臺運行。Activity 能夠用來啓動一個Service,Service啓動後能夠保持在後臺一直運行,即便啓動它的Activity退出或是切換到別的應用Service也能保持運行狀態。android
Service 能夠以兩種形式存在:app
雖然Service能夠有上述兩種表現形式,這只是爲了說明上的方便,實際上同一個Service能夠同時以兩種方式存在,只要實現兩種方法所定義的接口函數就能夠了。ide
建立一個Service,首先須要定義一個Service的子類,而後根據須要重載Service定義的一些核心方法:函數
定義了Service類和實現相應方法後,和Activity同樣,也須要在AndroidManifest.xml中定義這個Service:this
<manifest … > … <application … > <service android:name=」.ExampleService」 /> … < /application> < /manifest>
和Activity同樣,也能夠爲Service定義Intent Filter,若是你不想共享這個Service,能夠將android:exported屬性定義爲false。spa
一般狀況下Service在後臺運行,當Android也支持Service運行在前臺,運行在前臺的Service必須在屏幕頂端的Status Bar提供一個Notification以提示用戶有Service在運行。好比提供個Media Player使用的Service運行在前臺,而在標題欄顯示當前曲目。設計
本例Foreground Service Controller就顯示了一個在前臺運行的Service, 前臺運行的Service能夠經過調用startForeground()使Service在前臺運行。stopForeground中止前臺運行,但 Service自己不會中止。 startForeground,stopForeground是從2.0開始支持的,以前的版本採用setForeground。code
本例爲了支持2.0以前和2.0以後的版本,採用了Reflection的方法來來查找當前版本是否含有startForeground和stopForeground,若是有則調用,沒有則仍是使用setForeground。orm
若是找到的話,如下的變量用來存儲startForeground和stopForeground方法。和本例Service不相關,就不詳述了。 只要知道startForegroundCompat 和stopForegroundCompat的功能就是startForeground 和stopForeground就好了。xml
private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class}; private static final Class[] mStopForegroundSignature = new Class[] { boolean.class}; private Method mStartForeground; private Method mStopForeground; private Object[] mStartForegroundArgs = new Object[2]; private Object[] mStopForegroundArgs = new Object[1];
下面來看看ForegroundService的代碼:
首先是必須做爲Service的子類:
public class ForegroundService extends Service
由於是做爲「Started」 Service來設計的,所以需定義onStartCommand ,一樣onStartCommand也是在Android 2.0以後添加的,2.0以前爲onStart。本例爲了支持全部版本,兩個方法對實現了,對應2.0以後的版本,只會調用 onStartCommand,2.0以前的只會調用onStart:
// This is the old onStart method that // will be called on the pre-2.0 platform. // On 2.0 or later we override onStartCommand() so this // method will not be called. @Override public void onStart(Intent intent, int startId) { handleCommand(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { handleCommand(intent); // We want this service to //continue running until it is explicitly // stopped, so return sticky. return START_STICKY; }
onStartCommand 能夠有返回結果,這個返回值告訴Android系統當這個Service被Kill以後(好比當系統內存不足時)後續操做。START_STICKY 表示系統Kill這個Service以後,若是從新建立這個Service時在調用onStartCommand ,不會將最後的Intent做爲參數傳入,也就是說intent=null. START_REDELIVER_INTENT則會傳入被殺前未處理的最後一個Intent。
本Service不做爲Bind Service ,所以經過一個空實現:
@Override public IBinder onBind(Intent intent) { return null; }
最後看看如何啓動/中止這個Service, Controller 是做爲這個Service的控制類來實現的,提供了前臺啓動,後臺啓動,和中止Service操做:
private OnClickListener mForegroundListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(ForegroundService.ACTION_FOREGROUND); intent.setClass(Controller.this, ForegroundService.class); startService(intent); } }; private OnClickListener mBackgroundListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(ForegroundService.ACTION_BACKGROUND); intent.setClass(Controller.this, ForegroundService.class); startService(intent); } }; private OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { stopService(new Intent(Controller.this, ForegroundService.class)); } };