在 Android 开始时启动应用程序

Start App when Android Start

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
{...}
        <receiver
        android:enabled="true"
        android:exported="true"
        android:name="com.example.richard.Test.StartMyServiceAtBootReciever"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
{...}

StartMyServiceAtBootReciever.java

    package com.example.richard.Test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by Richard on 23/03/2016.
 */
public class StartMyServiceAtBootReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, MainActivity.class);
            serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(serviceIntent);
        }
    }
}

详细信息:当我重新启动 phone 时,应用程序没有一起启动。有人可以帮助我吗?

如果您要开始一个 Activity 然后使用

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
       Intent serviceIntent = new Intent(context, MainActivity.class);
       serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(serviceIntent);
 }