屏幕唤醒 + 屏幕解锁 + 开机广播android.intent.action.BOOT_COMPLETED

android.intent.action.BOOT_COMPLETED
1.内容说明


Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播  流程:广播接收器 (屏幕唤醒 + 屏幕解锁 + 接收开机广播)

代码如下:


        

/**
 * @Author: Su
 * @Date: 2022/11/30
 * @Description:广播接收器
 */
public class BootupReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("BootupReceiver---",intent.getAction());
        Toast.makeText(context,intent.getAction(),Toast.LENGTH_SHORT).show();
        //屏幕唤醒
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "BootBroadcastReceiver");
        wl.acquire();

        //屏幕解锁
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("BootBroadcastReceiver");
        kl.disableKeyguard();

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

            // context, AutoRun.class
            Intent newIntent = new Intent(context, MainActivity.class);

            /* MyActivity action defined in AndroidManifest.xml */
            newIntent.setAction("android.intent.action.MAIN");

            /*
             * If activity is not launched in Activity environment, this flag is
             * mandatory to set
             */
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            /* if you want to start a service, follow below method */
            context.startActivity(newIntent);

        }


    }
}
/**
 * @Author: Su
 * @Date: 2022/11/30
 * @Description:清单文件配置广播接收器
 */
<receiver
            android:name=".receiver.BootupReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </receiver>
/**
 * @Author: Su
 * @Date: 2022/11/30
 * @Description:添加权限
 */
    <!--    开机监听-->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <!--    唤起APP-->
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
       <!--    悬浮窗权限-->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

3.常见问题
Android API Level8以上的时候,程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到该广播。

4.解决方案
清单文件 目录下添加:


android:installLocation=“internalOnly”
 

5.android:installLocation说明
android:installLocation的属性有"auto"、“internalOnly”、“preferExternal”

auto:程序可能被安装在外部存储介质上(例如:SD Card),但是默认会被安装到手机内存中.当手机内存为空时,程序将被安装到外部存储介质上.当程序安装到手机上后,用户可以决定把程序放在外部储介质还是内存中.
internalOnly:默认值.当设置为该值时,程序只能被安装在内存中,如果内存为空,则程序将不能成功安装.
preferExternal:将程序安装在外部存储介质上,但是系统不保证程序一定会被安装到外部存储介质上.当外部存储介质不可以或空时,程序将被安装到内存中.程序使用了forward-locking机制时也将被安装到内存中,因为外部存储不支持此机制.程序安装后,用户可以自由切换程序应该在外部还是内部存储介质上.


————————————————
版权声明:本文为CSDN博主「Android程序Su」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_51522235/article/details/128163268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VhdManager.exe VhdManager.ini ; Generated (13.09.2015 18:44:54) by Simple Vhd Manager v1.0 ; www.sordum.org [Main] Language=Auto WinSetOnTop=0 GridLines=1 NewVhdSize=2,5 NewVhdOpt=0 [VirtualDiskFiles] 1=C:\20180619_034757.vhd [Language_English] 01="BlueLife" 02="Attach marked and detach unmarked files" 03="Add file to list" 04="Remove file from list" 05="Refresh list" 06="Disk Management" 07="Action\n Location\n Attached\n Boot Menu\n Virtual Size" 08="&File;\n &Edit;\n &Options;\n Tools \n &Help;" 09="&Add; File" 10="&Create; and Attach VHD" 11="&Attach; Marked Files" 12="&Detach; All Files" 13="&Remove; Entries In Boot Menu" 14="&Exit;" 15="&Attach;" 16="&Detach;" 17="&Read;-Only" 18="&Add; to Boot Menu" 19="&Remove; From Boot Menu" 20="&Paste; File" 21="&Remove;" 22="&Remove; All" 23="&Check; - Uncheck" 24="&Check; All" 25="&Uncheck; All" 26="&Open; Location" 27="&File; Properties" 28="&Disk;" 29="&Cd;-Rom" 30="&Refresh;" 31="&Open; With Explorer" 32="&Change; Drive Letter" 33="&Remove; Letter" 34="&Drive; Properties" 35="&Attach; Marked To System Startup" 36="&Disable; Autorun" 37="&Always; On Top" 38="&Show; Grid Lines" 39="&Add; to Context Menu" 40="&Show; Hidden Files" 41="&Show; File Extensions" 42="Disk &Management;" 43="Diskpart &Command; Prompt" 44="&Msconfig;" 45="&Restart; Windows Explorer" 46="&Donate;" 47="&HomePage;" 48="&Contact;" 49="&About;" 50="Please Wait ..." 51="Select Virtual Disk File\n Virtual Disk Files\n All Files" 52="Enter boot menu name\nOk\nCancel" 53="Added" 54="Unable to write file!" 55="File not found!" 56="The file path must not contain any spaces. Please correct it and try again." 57="Virtual hard disk size \nVirtual hard disk format \n Virtual hard disk type \nDynamically expanding \n Fixed" 58="%drive% not enough free space!\n Do you wish to continue?" 59="An error occurred. Check your settings and try again! \nError Code:%error%" 60="Mount &Options;\n &Mount; \n &Unmount; \n &Open; With Simple VHD Manager\n VHD Mount \n V

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值