Android8.0中怎么開啟service

本篇文章為大家展示了Android8.0中怎么開啟service,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、成都品牌網(wǎng)站建設(shè)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。成都創(chuàng)新互聯(lián)擁有實(shí)力堅(jiān)強(qiáng)的技術(shù)研發(fā)團(tuán)隊(duì)及素養(yǎng)的視覺設(shè)計(jì)專才。

原因

Android 8.0 有一項(xiàng)復(fù)雜功能;系統(tǒng)不允許后臺(tái)應(yīng)用創(chuàng)建后臺(tái)服務(wù)。 因此,Android 8.0 引入了一種全新的方法,即 Context.startForegroundService(),以在前臺(tái)啟動(dòng)新服務(wù)。 在系統(tǒng)創(chuàng)建服務(wù)后,應(yīng)用有5秒的時(shí)間來調(diào)用該服務(wù)的 startForeground() 方法以顯示新服務(wù)的用戶可見通知。如果應(yīng)用在此時(shí)間限制內(nèi)未調(diào)用 startForeground(),則系統(tǒng)將停止服務(wù)并聲明此應(yīng)用為 ANR。

遇到的問題

但是目前在調(diào)用:context.startForegroundService(intent)時(shí)報(bào)如下ANR,startForegroundService()文檔說明在service啟動(dòng)后要調(diào)用startForeground()。

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

完整解決步驟:

1. 添加權(quán)限

<!--android 9.0上使用前臺(tái)服務(wù),需要添加權(quán)限,此權(quán)限為級(jí)別為nomarl-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

2. 啟動(dòng)server(引用啟動(dòng)5秒內(nèi)要啟動(dòng)server)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       context.startForegroundService(new Intent(context, MyService.class));
   } else {
       context.startService(new Intent(context, MyService.class));
   }

然后必須在Myservice中調(diào)用startForeground():

3. Server中onCreate方法中調(diào)用startForeground()

public static final String CHANNEL_ID_STRING = "service_01";
private Notification notification;
   @Override
   public void onCreate() {
       super.onCreate();
       //適配8.0service
       NotificationManager notificationManager = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
       NotificationChannel mChannel = null;
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
           mChannel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name),
                   NotificationManager.IMPORTANCE_LOW);
           notificationManager.createNotificationChannel(mChannel);
           notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
           startForeground(1, notification);
       }
}

4. 在onStart里再次調(diào)用startForeground()

@Override
public void onStart(Intent intent, int startId) {
   super.onStart(intent, startId);

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
       startForeground(1, notification);
   }

}

注解:

  • Android 8.0 系統(tǒng)不允許后臺(tái)應(yīng)用創(chuàng)建后臺(tái)服務(wù),故只能使用Context.startForegroundService()啟動(dòng)服務(wù)

  • 創(chuàng)建服務(wù)后,應(yīng)用必須在5秒內(nèi)調(diào)用該服務(wù)的 startForeground() 顯示一條可見通知,聲明有服務(wù)在掛著,不然系統(tǒng)會(huì)停止服務(wù) + ANR 套餐送上。

  • Notification 要加 Channel,系統(tǒng)的要求

  • 為什么要在onStart里再次調(diào)用startForeground()?答:這一條主要是針對(duì)后臺(tái)?;畹姆?wù),如果在服務(wù)A運(yùn)行期間,?;顧C(jī)制又startForegroundService啟動(dòng)了一次服務(wù)A,那么這樣不會(huì)調(diào)用服務(wù)A的onCreate方法,只會(huì)調(diào)用onStart方法。如果不在onStart方法里再掛個(gè)通知的話,系統(tǒng)會(huì)認(rèn)為你使用了 startForegroundService 卻不在 5 秒內(nèi)給通知,很傻地就停止服務(wù) + ANR 套餐送上了。

上述內(nèi)容就是Android8.0中怎么開啟service,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:Android8.0中怎么開啟service
本文網(wǎng)址:http://bm7419.com/article44/igishe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化域名注冊(cè)、小程序開發(fā)、定制開發(fā)、外貿(mào)網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名