首页 > 代码库 > 安卓开机自启动demo

安卓开机自启动demo

package com.example.openstart;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MPUStartBroadcastReceiver extends BroadcastReceiver {

	/**
	 * 接收系统启动完成广播
	 * */
	@Override
	public void onReceive(Context context, Intent intent) {
		//取消系统锁屏	
		KeyguardManager mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
		KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock("MainActivity");
		mKeyguardLock.disableKeyguard();
		intent = new Intent(context, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);//启动意图
	}
}

manifest.xml配置

注册接收系统启动广播

<receiver android:name="com.example.openstart.MPUStartBroadcastReceiver" >
            <intent-filter android:priority="2147483647" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" >
                </category>
            </intent-filter>
        </receiver>
权限添加

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

RECEIVE_BOOT_COMPLETED系统启动完成广播

DISABLE_KEYGUARD允许程序禁用键盘锁