這篇文章主要介紹componentname怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
ComponentName,顧名思義,就是組件名稱,通過調(diào)用Intent中的setComponent方法,我們可以打開另外一個(gè)應(yīng)用中的Activity或者服務(wù)。
如何實(shí)例化一個(gè)ComponentName
實(shí)例化一個(gè)ComponentName需要兩個(gè)參數(shù),第一個(gè)參數(shù)是要啟動(dòng)應(yīng)用的包名稱,這個(gè)包名稱是指清單文件中列出的應(yīng)用的包名稱:
第二個(gè)參數(shù)是你要啟動(dòng)的Activity或者Service的全稱(包名+類名),代碼如下:
啟動(dòng)一個(gè)Activity:
Intentintent=newIntent();
intent.setComponent(newComponentName("com.example.otherapp",
"com.example.otherapp.MainActivity2"));
startActivity(intent);
ComponentName使用
ComponentName:可以啟動(dòng)其他應(yīng)用的Activity、Service.
ComponentNamechatActivity=newComponentName(param1,param2);
param1:Activity、Service所在應(yīng)用的包名
param2:Activity、Service的包名+類名
Activity:
ComponentNamechatActivity=newComponentName("com.npf.chat","com.npf.chat.ui.ChatActivity");
Intentintent=newIntent();
intent.setComponent(chatActivity);
startActivity(intent);
Service:
ComponentNamechatService=newComponentName("com.npf.chat","com.npf.chat.ui.ChatService");
Intentintent=newIntent();
intent.setComponent(chatService);
startService(intent);
注:
如果該Activity非應(yīng)用入口(入口Activity默認(rèn)android:exported="true"),則需要再清單文件中添加android:exported="true"。Service也需要添加android:exported="true"。允許外部應(yīng)用調(diào)用。
以上是“componentname怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站標(biāo)題:componentname怎么用-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://chinadenli.net/article0/deieoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站策劃、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)