Android启动时启动应用程序

时间:2016-03-23 20:27:28

标签: java android launcher

的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);
        }
    }
}

详细信息:当我重新启动手机时,应用程序无法一起启动。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:3)

如果您要开始活动,请使用

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);
 }