新聞中心
一般情況下,Service 都是在后臺運行的,后臺 Service 的優(yōu)先級都是比較低的,當(dāng)資源不足時,系統(tǒng)有可能回收正在運行的后臺 Service。

成都創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)福貢,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
在一些場景下(如播放音樂),用戶希望應(yīng)用能夠一直保持運行,此時就需要使用前臺 Service。前臺 Service 會始終保持正在運行的圖標(biāo)在系統(tǒng)狀態(tài)欄顯示。
使用前臺 Service 并不復(fù)雜,開發(fā)者只需在 Service 創(chuàng)建的方法里,調(diào)用 keepBackgroundRunning()將 Service 與通知綁定。調(diào)用 keepBackgroundRunning() 方法前需要在配置文件中聲明 ohos.permission.KEEP_BACKGROUND_RUNNING 權(quán)限,該權(quán)限是 normal 級別,同時還需要在配置文件中添加對應(yīng)的 backgroundModes 參數(shù)。在 onStop() 方法中調(diào)用 cancelBackgroundRunning() 方法可停止前臺 Service。
使用前臺 Service 的 onStart() 代碼示例如下:
// 創(chuàng)建通知,其中1005為notificationId
NotificationRequest request = new NotificationRequest(1005);
NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
content.setTitle("title").setText("text");
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
request.setContent(notificationContent);
// 綁定通知,1005為創(chuàng)建通知時傳入的notificationId
keepBackgroundRunning(1005, request);
在配置文件中配置如下:
{
"name": ".ServiceAbility",
"type": "service",
"visible": true,
"backgroundModes": ["dataTransfer","location"]
} 網(wǎng)站欄目:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS前臺Service
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/cdoshhi.html


咨詢
建站咨詢
