Android应用开机自启

1 权限
2 广播接收器

public class AutoStartBroadcastReceiver extends BroadcastReceiver
{
    public AutoStartBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activity = new Intent(context, MainActivity.class);
            activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activity);
        }
    }
}

3 广播接收器注册

        <receiver
            android:name=".AutoStartBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            intent-filter>
        receiver>

4 手机设置中允许开机启动。比如华为:设置 —> 全部设置—>开机自动启动
Android应用开机自启_第1张图片

你可能感兴趣的:(android)