新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS前臺Service
一般情況下,Service 都是在后臺運行的,后臺 Service 的優(yōu)先級都是比較低的,當資源不足時,系統(tǒng)有可能回收正在運行的后臺 Service。

成都創(chuàng)新互聯(lián)主要從事網站制作、成都做網站、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務福貢,10年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
在一些場景下(如播放音樂),用戶希望應用能夠一直保持運行,此時就需要使用前臺 Service。前臺 Service 會始終保持正在運行的圖標在系統(tǒng)狀態(tài)欄顯示。
使用前臺 Service 并不復雜,開發(fā)者只需在 Service 創(chuàng)建的方法里,調用 keepBackgroundRunning()將 Service 與通知綁定。調用 keepBackgroundRunning() 方法前需要在配置文件中聲明 ohos.permission.KEEP_BACKGROUND_RUNNING 權限,該權限是 normal 級別,同時還需要在配置文件中添加對應的 backgroundModes 參數。在 onStop() 方法中調用 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"]
} 新聞名稱:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS前臺Service
文章起源:http://m.5511xx.com/article/cdoshhi.html


咨詢
建站咨詢
