文件结构
在 AndroidManifest.xml 中添加权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zyju.cmaketest1"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />//广播权限 <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />//添加禁用权限 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".BootCompleteReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" />//添加开机广播接收器 </intent-filter> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity"></activity> </application> </manifest>
广播接收器类
package com.example.zyju.cmaketest1; import android.app.KeyguardManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * Created by GCS on 2016/10/20. */ public class BootCompleteReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //得到键盘锁管理器对象 KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //参数是LogCat里用的Tag KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock"); //解锁 kl.disableKeyguard(); Intent intent1=new Intent(context,MainActivity.class); Toast.makeText(context,"启动成功",Toast.LENGTH_LONG).show(); intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } }