开机启动(无序广播)
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>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!