Android中各種Service
1. 後臺服務
後臺服務分為可互動服務和不可互動服務。其區別在於啟動服務的方式 StartService()
和 bindService()
。後者會返回 Bind
物件供 Service
中方法和處理結果,而前者不會。
1.1 不可互動服務
三個方法:
-
onCreate
,用於初始化服務,只會被呼叫一次。 -
onStartService
,用於執行任務,每次呼叫Context.startService(context,Service.class)
都會呼叫。 -
onDestory
,呼叫Context.stopService(context,Service.class)
結束服務的時候呼叫。
public class NoInterrationService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { Log.d("Service","onBind"); return null; } @Override public void onCreate() { super.onCreate(); Log.d("Service","onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("Service","onStartCommand"); new Thread(new Runnable() { @Override public void run() { // 耗時操作 } }).start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d("Service","onDestroy"); super.onDestroy(); } }

無互動服務的生命週期
1.2 可互動服務
利用ConnectionService類和Service互動。
啟動可互動服務 bindService(new Intent(getApplicationContext(), InteractionService.class), connection, BIND_AUTO_CREATE);
。三個引數, intent
用於指定要啟動的服務名稱, ServiceConnection
用於互動, Flag
設定服務型別。
ServiceConnection :
ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("Service", "onServiceConnected"); InteractionService.MyBinder binder = (InteractionService.MyBinder) service; binder.func1(); unbindService(this); //關閉服務 } @Override public void onServiceDisconnected(ComponentName name) { } };
} public class InteractionService extends Service { @Override public void onCreate() { super.onCreate(); Log.d("Service", "onCreate"); } @Nullable @Override public IBinder onBind(Intent intent) { Log.d("Service", "onBind"); return new MyBinder(); } class MyBinder extends Binder { public void func1() { Log.d("Service", "func1"); Log.d("Service", "thread id " + Thread.currentThread().getId()); } } @Override public boolean onUnbind(Intent intent) { Log.d("Service", "onUnBind"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.d("Service", "onDestroy"); super.onDestroy(); } }

服務的生命週期
BindService
之後並沒有呼叫
Binder
中定義的
func
。是因為開啟服務是非同步的,並不會立即執行
ServiceConnection.onServiceConnected()
方法,我想內部實現應該是訊息佇列,將
BindService
操作傳送給主執行緒的
Looper
,等待主執行緒處理這個訊息的時候,再執行
ServiceConnection.onServiceConnection
中的方法。
2. 混合性互動的後臺服務
或許你會迷惑, startService
和 bindService
之間有什麼關係?其實簡單的說兩者之間是沒有關聯的,類似於你親媽生了個雙胞胎一樣。混合使用,其完整的生命週期是: onCreate->onStartCommand->onBind->onUnBind->onDestroy
。
3. 前臺服務
由於後臺服務優先順序相對比較低,當系統出現記憶體不足的情況下,它就有可能會被回收掉,所以前臺服務就是來彌補這個缺點的,它可以一直保持執行狀態而不被系統回收。例如:墨跡天氣在狀態列中的天氣預報

前臺服務

簡單的前臺服務實現
4. IntentService
IntentService是專門用來解決Service中不能執行耗時操作這一問題的,建立一個IntentService也很簡單,只要繼承IntentService並覆寫onHandlerIntent函式,在該函式中就可以執行耗時操作了。
IntentService的具體實現將在下一遍文章中介紹。
public class MyIntentService extends IntentService { /** * Creates an IntentService.Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public MyIntentService(String name) { super(name); } @Override protected void onHandleIntent(@Nullable Intent intent) { // 處理耗時操作,已近開起來新的執行緒 } }
5. 系統服務
系統服務提供了很多便捷服務,可以查詢Wifi、網路狀態、查詢電量、查詢音量、查詢包名、查詢Application資訊等等等相關多的服務,具體大家可以自信查詢文件,這裡舉例2個常見的服務。
Wifi
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE); boolean enabled = wm.isWifiEnabled();
最大音量
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); int max = am.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);