Android 开机自启动

APP需要开机自启动,要通过开机广播实现。

1,在AndroidManifest.xml中增加权限

    
    

2,在AndroidManifest.xml中application标签内增加开机广播

     

            
            
                
            

        

3,增加开机广播实现类,其中MainActivity.class是开机启动页面

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Intent thisIntent = new Intent(context, MainActivity.class);
            thisIntent.setAction("android.intent.action.MAIN");
            thisIntent.addCategory("android.intent.category.LAUNCHER");
            thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(thisIntent);
        }
    }
}

你可能感兴趣的:(Android,android)