Android App开机启动是很多应用所需求的功能。比如说,短信应用就需要开机启动:

而开机启动的实现很简单:

1. 实现开机启动的BootBroadcastReceiver:

public class BootBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BootBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Log.d(TAG, "BootBroadcastReceiver has received");
            // 接收到开机启动的广播后,可在这里做你想做的事情!如:启动的一个服务service
            context.startService(new Intent(context, AppService.class));
        }
    }

}

2. 在Androidmenifest中注册该广播,并加上开启动的Action:

        <receiver android:name="org.chuanyang.android.demo.callphone.BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

3. 最后,当然也最为重要的一点,记得加上开机启动权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />