美文网首页
Androd 开机自启动

Androd 开机自启动

作者: CQ_TYL | 来源:发表于2021-03-05 10:38 被阅读0次

    使用:

    //屏幕解锁解锁权限
        <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    //清单文件注册
       <receiver android:name=".service.StartupReceiver">
                <intent-filter android:priority="1000">
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                </intent-filter>
            </receiver>
    
    package com.jetshine.room_flat_moudle.service;
    import android.annotation.SuppressLint;
    import android.app.KeyguardManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.PowerManager;
    import android.util.Log;
    
    import com.jetshine.room_flat_moudle.MainActivity;
    /**
     * Created by shinelon on 2018/9/18.
     */
    
    public class StartupReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("jjc",intent.getAction());
    
            //开机后一般会停留在锁屏页面且短时间内没有进行解锁操作屏幕会进入休眠状态,此时就需要先唤醒屏幕和解锁屏幕
            //屏幕唤醒
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            @SuppressLint("InvalidWakeLockTag")
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.SCREEN_DIM_WAKE_LOCK, "StartupReceiver");//最后的参数是LogCat里用的Tag
            wl.acquire();
    
            //屏幕解锁
            KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardManager.KeyguardLock kl = km.newKeyguardLock("StartupReceiver");//参数是LogCat里用的Tag
            kl.disableKeyguard();
    
            //开机启动
            Intent mainIntent = new Intent(context, MainActivity.class);
            //在BroadcastReceiver中显示Activity,必须要设置FLAG_ACTIVITY_NEW_TASK标志
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mainIntent);
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Androd 开机自启动

          本文链接:https://www.haomeiwen.com/subject/nalrqltx.html