android - 在 Android 中启动设备时应用程序不会自动启动应用程序

标签 android broadcastreceiver startup

我已经设法实现了以下方法编码,允许应用程序在设备启动/启动时执行自动启动。但是,当我测试实现代码时,它无法工作,应用程序无法在设备启动时执行自动启动。任何人都可以帮助或建议我有什么可能性吗?谢谢。

list .xml

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

<receiver android:enabled="true" android:exported="true"
          android:name="com.dapoaugury.apps.robotapp.AutoStartUp"
          android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
          android:installLocation="internalOnly">
    <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>

自动启动.java

package com.dapoaugury.apps.robotapp;


/**
  * To Auto-Start Application on Device Start-up/ Boot
  * Created by dev02 on 10/7/15.
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoStartUp extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){
            //To start new intent when phone starts up
            Intent i = new Intent(context, MainActivity.class);
            // To put activity on the top of the stack since activity is launched from context outside activity
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // EDITED
            i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            context.startActivity(i);
        }
        //To Start Application on Phone Start-up - 10/7/2015(END OF VERSION)
    }
}

MainActivity.java(假定在首次安装应用程序时手动启动的机制)

public class MainActivity extends Activity {


public static long AppElapsedTime;
public static long AppElapseTime_hr;
public static long Process_startTime = System.nanoTime();
public static long CurrentProcTime;
private static Context context;

public final static int GREEN = 0;
public final static int BLUE = 1;

private static int cTheme = GREEN;

WebView webView;
ProgressBar pb;

@Override
public void onCreate(Bundle savedInstanceState) {
...........
}

最佳答案

新安装的应用程序将处于“已停止”状态,直到该应用程序真正首次启动。在此状态下,您的任何组件都不会被激活,因此您的 BOOT_COMPLETED 接收器将不会运行。您需要包含一个 Activity 并让用户打开它;您始终可以稍后使用 PackageManager 禁用 Activity。

此行为是在 Android 3.1 中引入的,您可以阅读相关内容 in the release notes (在“对已停止的应用程序启动控制”标题下)。

关于android - 在 Android 中启动设备时应用程序不会自动启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353411/

相关文章:

java - TelephonyManager 处于摘机状态时,电话号码设置为空

android - 如何在我的应用程序中获取组织工作配置文件的电话记录历史记录(发布为私有(private)应用程序)

c++ - 在启动时运行的 Qt 程序中读取文本文件

mongodb - 使用嵌入式 Mongodb 进行 Spring Boot 测试的启动速度非常慢

android - 如何构建共享库并在其他ndk程序中调用

java - 安卓 :Activity is blocking with black screen when it occur Exception

android - 在不删除旧数据的情况下向 FTP 服务器中的文件添加内容?

java - 检测 Android 应用升级并设置应用程序类 boolean 值以显示/隐藏 EULA

java - 拖放 - java.lang.ArrayIndexOutOfBoundsException

java - 如何开始使用liferay