1 前臺服務
因為服務的優先級較低,所以當系統內存不足時,可能會回收正在后臺運行的服務。如果若要避免服務被回收,可以使用前臺服務。
前臺服務會一直有一個圖標在系統的狀態欄中顯示,下拉狀態欄可以看到更加詳細的信息,類似于消息通知效果。
public class FirstService extends Service { private static final String TAG = "FirstService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); //設置為前臺服務 Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("梅西生涯最大尷尬 戰法國能否破荒?") .setContentText("世界杯1/8決賽,法國對陣阿根廷,法國隊主帥德尚將迎來80戰里程碑,成為隊史執教場次最多的主教練,高盧雄雞能否保持過去40年世界杯遇南美球隊不敗的金身,格里茲曼能否找回最佳狀態,梅西能否打破此前世界杯淘汰賽666分鐘的進球荒,都是此役的關鍵看點。") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); startForeground(1,notification); }}
在此構建出通知對象(Notification)之后,調用 startForeground() 讓當前服務變為一個前臺服務。
startForeground 接收兩個參數:
參數 | 說明 |
---|---|
id | 通知 ID |
Notification | Notification 對象 |
效果:
2 IntentService
如果在服務中處理耗時操作,那么容易出現 ANR(Application Not Responding)問題。
為了避免我們可以在主服務的具體方法中開啟子線程,然后在子線程中來執行耗時操作,形如:
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); //在子線程中來執行耗時操作 new Thread(new Runnable() { @Override public void run() { //耗時操作 } }).start(); return super.onStartCommand(intent, flags, startId);}
這樣的服務一旦啟動后,就會一直處于運行狀態,直到調用 stopService() 或者 stopSelf() 才會停止服務。我們可以在耗時操作執行完畢后,調用 stopSelf() ,讓服務自行停止:
new Thread(new Runnable() { @Override public void run() { //耗時操作 stopSelf(); }}).start();
Android 提供了 IntentService 類,可以直接創建一個異步、執行完畢會自行結束的服務。
我們新建一個類,讓它繼承自 IntentService :
public class SecondService extends IntentService { private static final String TAG = "SecondService"; public SecondService() { super("SecondService"); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "子線程 id(Intent 服務): " + Thread.currentThread().getId()); //在此執行耗時邏輯 } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); }}
注意:這個類必須提供一個無參構造函數,并且必須在這個構造函數內部調用父類的有參構造函數。
接著,在活動類中啟動 Intent 服務:
Log.d(TAG, "主線程 id: " + Thread.currentThread().getId());Intent intentService = new Intent(context, SecondService.class);startService(intentService);
輸出結果:
D/MainActivity: 主線程 id: 1
D/SecondService: 子線程 id(Intent 服務): 145
D/SecondService: onDestroy
從結果中可以看出,IntentService 服務類開啟了一個新的線程來執行耗時邏輯,并且在執行完畢后自動停止。是不是很方便呀O(∩_∩)O哈哈~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答