欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)

 更新時(shí)間:2022年05月17日 08:39:19   作者:?jiǎn)鑶柙脑? 
這篇文章主要為大家詳細(xì)介紹了android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近在做個(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)文章

最新評(píng)論