掃二維碼與項(xiàng)目經(jīng)理溝通
我們在微信上24小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
介紹通過startAbility()啟動(dòng)Service以及對應(yīng)的停止方法。

創(chuàng)新互聯(lián)專注于田陽網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供田陽營銷型網(wǎng)站建設(shè),田陽網(wǎng)站制作、田陽網(wǎng)頁設(shè)計(jì)、田陽網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造田陽網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供田陽網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
Ability為開發(fā)者提供了 startAbility() 方法來啟動(dòng)另外一個(gè) Ability。因?yàn)镾ervice也是 Ability 的一種,開發(fā)者同樣可以通過將 Intent 傳遞給該方法來啟動(dòng) Service。不僅支持啟動(dòng)本地 Service,還支持啟動(dòng)遠(yuǎn)程 Service。
開發(fā)者可以通過構(gòu)造包含 DeviceId、BundleName 與 AbilityName 的 Operation 對象來設(shè)置目標(biāo) Service 信息。這三個(gè)參數(shù)的含義如下:
啟動(dòng)本地設(shè)備 Service 的代碼示例如下:
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.huawei.hiworld.himusic")
.withAbilityName("com.huawei.hiworld.himusic.entry.ServiceAbility")
.build();
intent.setOperation(operation);
startAbility(intent);啟動(dòng)遠(yuǎn)程設(shè)備 Service 的代碼示例如下:
Operation operation = new Intent.OperationBuilder()
.withDeviceId("deviceId")
.withBundleName("com.huawei.hiworld.himusic")
.withAbilityName("com.huawei.hiworld.himusic.entry.ServiceAbility")
.withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) // 設(shè)置支持分布式調(diào)度系統(tǒng)多設(shè)備啟動(dòng)的標(biāo)識(shí)
.build();
Intent intent = new Intent();
intent.setOperation(operation);
startAbility(intent);執(zhí)行上述代碼后,Ability 將通過 startAbility() 方法來啟動(dòng) Service。
Service 一旦創(chuàng)建就會(huì)一直保持在后臺(tái)運(yùn)行,除非必須回收內(nèi)存資源,否則系統(tǒng)不會(huì)停止或銷毀 Service。開發(fā)者可以在 Service 中通過 terminateAbility() 停止本 Service 或在其他 Ability 調(diào)用 stopAbility() 來停止 Service。
停止 Service 同樣支持停止本地設(shè)備 Service 和停止遠(yuǎn)程設(shè)備 Service,使用方法與啟動(dòng) Service 一樣。一旦調(diào)用停止 Service 的方法,系統(tǒng)便會(huì)盡快銷毀 Service。

我們在微信上24小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流