package com.example.start; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class StartReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){ System.out.println("手机开机了!!"); //开启MainActivity Intent intent2 = new Intent(context, MainActivity.class); //把MainActivity放到任务栈里 intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent2); } } }
清单文件中: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="com.example.start.StartReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
时间: 2024-10-17 15:06:31