您好,登錄后才能下訂單哦!
1 前臺(tái)服務(wù)
因?yàn)榉?wù)的優(yōu)先級(jí)較低,所以當(dāng)系統(tǒng)內(nèi)存不足時(shí),可能會(huì)回收正在后臺(tái)運(yùn)行的服務(wù)。如果若要避免服務(wù)被回收,可以使用前臺(tái)服務(wù)。
前臺(tái)服務(wù)會(huì)一直有一個(gè)圖標(biāo)在系統(tǒng)的狀態(tài)欄中顯示,下拉狀態(tài)欄可以看到更加詳細(xì)的信息,類似于消息通知效果。
public class FirstService extends Service { private static final String TAG = "FirstService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); //設(shè)置為前臺(tái)服務(wù) Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("梅西生涯最大尷尬 戰(zhàn)法國能否破荒?") .setContentText("世界杯1/8決賽,法國對(duì)陣阿根廷,法國隊(duì)主帥德尚將迎來80戰(zhàn)里程碑,成為隊(duì)史執(zhí)教場(chǎng)次最多的主教練,高盧雄雞能否保持過去40年世界杯遇南美球隊(duì)不敗的金身,格里茲曼能否找回最佳狀態(tài),梅西能否打破此前世界杯淘汰賽666分鐘的進(jìn)球荒,都是此役的關(guān)鍵看點(diǎn)。") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); startForeground(1,notification); } }
在此構(gòu)建出通知對(duì)象(Notification)之后,調(diào)用 startForeground() 讓當(dāng)前服務(wù)變?yōu)橐粋€(gè)前臺(tái)服務(wù)。
startForeground 接收兩個(gè)參數(shù):
參數(shù) | 說明 |
---|---|
id | 通知 ID |
Notification | Notification 對(duì)象 |
效果:
2 IntentService
如果在服務(wù)中處理耗時(shí)操作,那么容易出現(xiàn) ANR(Application Not Responding)問題。
為了避免我們可以在主服務(wù)的具體方法中開啟子線程,然后在子線程中來執(zhí)行耗時(shí)操作,形如:
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); //在子線程中來執(zhí)行耗時(shí)操作 new Thread(new Runnable() { @Override public void run() { //耗時(shí)操作 } }).start(); return super.onStartCommand(intent, flags, startId); }
這樣的服務(wù)一旦啟動(dòng)后,就會(huì)一直處于運(yùn)行狀態(tài),直到調(diào)用 stopService() 或者 stopSelf() 才會(huì)停止服務(wù)。我們可以在耗時(shí)操作執(zhí)行完畢后,調(diào)用 stopSelf() ,讓服務(wù)自行停止:
new Thread(new Runnable() { @Override public void run() { //耗時(shí)操作 stopSelf(); } }).start();
Android 提供了 IntentService 類,可以直接創(chuàng)建一個(gè)異步、執(zhí)行完畢會(huì)自行結(jié)束的服務(wù)。
我們新建一個(gè)類,讓它繼承自 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 服務(wù)): " + Thread.currentThread().getId()); //在此執(zhí)行耗時(shí)邏輯 } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } }
注意:這個(gè)類必須提供一個(gè)無參構(gòu)造函數(shù),并且必須在這個(gè)構(gòu)造函數(shù)內(nèi)部調(diào)用父類的有參構(gòu)造函數(shù)。
接著,在活動(dòng)類中啟動(dòng) Intent 服務(wù):
Log.d(TAG, "主線程 id: " + Thread.currentThread().getId()); Intent intentService = new Intent(context, SecondService.class); startService(intentService);
輸出結(jié)果:
D/MainActivity: 主線程 id: 1
D/SecondService: 子線程 id(Intent 服務(wù)): 145
D/SecondService: onDestroy
從結(jié)果中可以看出,IntentService 服務(wù)類開啟了一個(gè)新的線程來執(zhí)行耗時(shí)邏輯,并且在執(zhí)行完畢后自動(dòng)停止。是不是很方便呀O(∩_∩)O哈哈~
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。