<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lidaochen.test001"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver> </application> </manifest>
package com.lidaochen.test001; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.SystemClock; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { // 下面的这个方法是屏蔽掉了程序的后退键 @Override public void onBackPressed() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
package com.lidaochen.test001; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BootReceiver extends BroadcastReceiver { // 当手机重新启动的时候调用 @Override public void onReceive(Context context, Intent intent) { Log.e("boot", "进来了!"); // 在这个方法里面开启Activity Intent intent1 = new Intent(context, MainActivity.class); // 注意不能在广播接收者里面直接开启Activity,需要添加一个任务栈的标记, intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 开启Activity context.startActivity(intent1); } }