android四大组件之BroadcastReceiver开机启动
和其他BroadcastReceiver一样需要配置接收的广播信息,然后配置权限
<receiver
android:name="com.example.kaijistart.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
然后就在BroadcastReceiver里进行intent跳转就可以了
package com.example.kaijistart; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootReceiver extends BroadcastReceiver { public BootReceiver() { } @Override public void onReceive(Context context, Intent intent) { Intent it = new Intent(context,MainActivity.class); //设置新的任务栈,如果我们从系统图标点进去,系统会创建activity栈, //但是我们现在没有点击就要自己创建任务栈了 it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); System.out.println("开机了"); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.kaijistart" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.kaijistart.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.kaijistart.BootReceiver" > <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest>