android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)
最近在做個(gè)APP,需要開啟自啟功能,通過(guò)在網(wǎng)上查找資料,實(shí)現(xiàn)了自啟功能,非常簡(jiǎn)單,步驟如下:
1、創(chuàng)建廣播接收器broadcastReceiver
2、在AndroidManifest.xml中配置自啟權(quán)限和注冊(cè)接收器接收的廣播消息類型
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver ? ? android:name=".reveiver.StartReceiver" ? ? android:enabled="true" ? ? android:exported="true"> ? ? <intent-filter> ? ? ? ? <action android:name="android.intent.action.MEDIA_MOUNTED"/> ? ? ? ? <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> ? ? ? ? <action android:name="android.intent.action.MEDIA_EJECT"/> ? ? ? ? <action android:name="android.intent.action.MEDIA_REMOVED"/> ? ? ? ? <data android:scheme="file" /> ? ? </intent-filter> ? ? <intent-filter> ? ? ? ? <action android:name="android.intent.action.BOOT_COMPLETED"/> ? ? </intent-filter> </receiver>
因?yàn)锳ndroid API Level8以上的時(shí)候,程序可以安裝在SD卡上。如果程序安裝在SD卡上,那么在BOOT_COMPLETED廣播發(fā)送之后,SD卡才會(huì)掛載,因此程序無(wú)法監(jiān)聽到BOOT_COMPLETED廣播。
如果BOOT_COMPLETED和MEDIA_MOUNTED,MEDIA_EJECT寫在同一個(gè)intent-filter中,那么無(wú)法檢測(cè)到BOOT_COMPLETED,對(duì)于沒(méi)有SD卡的手機(jī),只能檢測(cè)BOOT_COMPLETED,這樣就會(huì)導(dǎo)致無(wú)法檢測(cè)到開機(jī)了。要解決此問(wèn)題,同時(shí)監(jiān)聽開機(jī)和sd卡掛載就可以了,所以要分別放到了兩個(gè)intent-filter中。
3.在第1步創(chuàng)建的廣播接收器中實(shí)現(xiàn)自啟動(dòng)代碼
package com.example.administrator.mm_scan.reveiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import com.example.administrator.mm_scan.MainActivity; public class StartReceiver extends BroadcastReceiver { ? ? private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; ? ? private final String ACTION_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED"; ? ? private final String ACTION_MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED"; ? ? private final String ACTION_MEDIA_EJECT = "android.intent.action.MEDIA_EJECT"; ? ? private final String ACTION_MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED"; ? ? @Override ? ? public void onReceive(Context context, Intent intent) { ? ? ? ? // 判斷是否是系統(tǒng)開啟啟動(dòng)的消息,如果是,則啟動(dòng)APP ? ? ? ? if ( ? ?ACTION_BOOT.equals(intent.getAction()) || ? ? ? ? ? ? ? ? ACTION_MEDIA_MOUNTED.equals(intent.getAction()) || ? ? ? ? ? ? ? ? ACTION_MEDIA_UNMOUNTED.equals(intent.getAction()) || ? ? ? ? ? ? ? ? ACTION_MEDIA_EJECT.equals(intent.getAction()) || ? ? ? ? ? ? ? ? ACTION_MEDIA_REMOVED.equals(intent.getAction()) ? ? ? ? ) { ? ? ? ? ? ? Intent intentMainActivity = new Intent(context, MainActivity.class); ? ? ? ? ? ? intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ? ? ? ? ? ? context.startActivity(intentMainActivity); ? ? ? ? } ? ? } }
完成以上步驟,APP就能實(shí)現(xiàn)開啟自啟了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 2.3.7.r1 camera錄像過(guò)程中按menu菜單鍵時(shí)會(huì)停止錄像
android GB版本的camera錄像過(guò)程中按“菜單”鍵會(huì)停止錄像,改成錄像時(shí)按menu鍵不做處理,具體修改方法如下,感興趣的朋友可以參考下哈2013-06-06詳解Android TableLayout中stretchColumns、shrinkColumns的用法
這篇文章主要介紹了Android TableLayout中stretchColumns、shrinkColumns用法的相關(guān)資料,需要的朋友可以參考下2017-03-03RxJava實(shí)戰(zhàn)之訂閱流基本原理示例解析
這篇文章主要為大家介紹了RxJava實(shí)戰(zhàn)之訂閱流基本原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12android教程之textview解析帶圖片的html示例
本文介紹的示例適用于android中需要解析帶圖片的htlm數(shù)據(jù),需要的朋友可以參考下2014-02-02Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用
Retrofit和OkHttp API以及JVM擴(kuò)展RxJava都是開源項(xiàng)目,大家可以輕松在GitHub上找到,下載和基本配置部分這里我們不作重點(diǎn),主要還是來(lái)看一下Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用:2016-06-06Android實(shí)現(xiàn)創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)執(zhí)行語(yǔ)句
這篇文章主要介紹了Android實(shí)現(xiàn)創(chuàng)建或升級(jí)數(shù)據(jù)庫(kù)時(shí)執(zhí)行語(yǔ)句,是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08android自定義ListView實(shí)現(xiàn)底部View自動(dòng)隱藏和消失的功能
本篇文章主要介紹了android自定義ListView實(shí)現(xiàn)底部View自動(dòng)隱藏和消失的功能 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android自定義View實(shí)現(xiàn)BMI指數(shù)條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)BMI指數(shù)條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06