這篇“Android中如何實現(xiàn)開機自啟動”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android中如何實現(xiàn)開機自啟動”文章吧。

1.定義一個BroadcastReceiver
Java代碼
public class BootReceiver extends BroadcastReceiver { public void onReceive(Context ctx, Intent intent) { Log.d("BootReceiver", "system boot completed"); //start activity String action="android.intent.action.MAIN"; String category="android.intent.category.LAUNCHER"; Intent myi=new Intent(ctx,CustomDialog.class); myi.setAction(action); myi.addCategory(category); myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(myi); //start service Intent s=new Intent(ctx,MyService.class); ctx.startService(s); } } public class BootReceiver extends BroadcastReceiver { public void onReceive(Context ctx, Intent intent) { Log.d("BootReceiver", "system boot completed"); //start activity String action="android.intent.action.MAIN"; String category="android.intent.category.LAUNCHER"; Intent myi=new Intent(ctx,CustomDialog.class); myi.setAction(action); myi.addCategory(category); myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(myi); //start service Intent s=new Intent(ctx,MyService.class); ctx.startService(s); } }2.配置Receiver的許可,允許接收系統(tǒng)啟動消息,在AndroidManifest.xml中:
Xml代碼
< uses-permission android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"/>< uses-permission android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"/>
3.配置Receiver,可以接收系統(tǒng)啟動消息,在AndroidManifest.xml中
Android開機自啟動的Xml代碼
< receiver android:name=".app.BootReceiver"> < intent-filter> < action android:name="android.intent.action.BOOT_COMPLETED"/> < category android:name="android.intent.category.HOME" /> < /intent-filter> < /receiver> < receiver android:name=".app.BootReceiver"> < intent-filter> < action android:name="android.intent.action.BOOT_COMPLETED"/> < category android:name="android.intent.category.HOME" /> < /intent-filter> < /receiver>
以上就是關(guān)于“Android中如何實現(xiàn)開機自啟動”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:Android中如何實現(xiàn)開機自啟動-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article44/cogshe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、ChatGPT、微信公眾號、關(guān)鍵詞優(yōu)化、外貿(mào)建站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容