在Android中启动设备时,应用程序不会自动启动应用程序 [英] App doesn't auto-start an app when booting the device in Android

查看:241
本文介绍了在Android中启动设备时,应用程序不会自动启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法实现了以下方法编码,该方法编码允许应用程序在启动/启动设备时执行自动启动。但是,当我测试实现代码时,它无法正常工作,该应用程序在启动设备时无法执行自动启动。谁能帮忙或建议我可能的可能性是什么?谢谢。

I have managed to implement the following method coding that allows the app to perform an auto-launch when the device is booted/started-up. However, when I tested out the implementation code, it failed to work, the app has failed to perform an auto-launch when the device is booted. Can anyone please help or advice me what could be the possibilities? Thank you.

Manifest.xml

Manifest.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>

AutoStartup.java

AutoStartup.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(应该是在首次安装应用程序时手动启动的机制)

MainActivity.java (Mechanism that is suppose to be manually launch when app is first installed)

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) {
...........
}


推荐答案

从Android 3.1开始,新安装的应用始终处于停止状态,将其移出停止状态的唯一方法是至少手动启动应用程序的任何活动。

From Android 3.1, newly installed apps are always put into a "stopped" state and the only way to move it out of the stopped state is to manually launch any Activity of the app at least once.

对于您的问题,

1)您需要设计一种机制,使用户在安装应用程序后需要首先手动打开该应用程序的任何活动。

1) You need to design a mechanism where the user needs to first open any Activity of the app manually after installing the app.

2)之后,您的BootReceiver将正常运行,并且能够自动启动该应用程序的任何Activity。您的实现是绝对正确的。

2) After that, your BootReceiver will work correctly and it will be able to launch any Activity of that app automatically. Your implementation is absolutely correct.

我在其中一个应用中遇到了同样的问题,每次设备启动时我都试图打开一个Activity,但不适用于新安装的应用。用户至少手动打开一次应用程序后,该应用程序将退出停止状态,并且一切正常。

I faced the same problem in one of my apps, where I was trying to open an Activity every time the device boots but it didn't work for a newly installed app. Once the user opens the app manually at least once, the app moves out of "stopped" state and everything works normally.

编辑

1)请确保< uses-permission> < manifest> 标记。

2)请确保指定 android: installLocation = internalOnly 否则,如果将应用程序安装在SD卡中,则您将不会收到任何启动完成操作。

2) Please ensure that you specify android:installLocation="internalOnly" otherwise you will not receive any Boot Complete actions if the app is installed in the SD card.

3)与我一样

从Android 3.1开始,所有应用都处于 stopped状态,这与用户强制关闭

From Android 3.1, all apps are put in the stopped state which is the same as when user Force Closes any app.

在这种状态下,该应用程序不会由于任何原因自动运行,除非并且除非用户从

While in this state, the application will not run automatically for any reason, until and unless launched manually by the user from the launcher.

意思是直到用户手动启动应用程序,您才会收到 ACTION_PACKAGE_INSTALLED,BOOT_COMPLETED 等。 Google做出此决定是为了防止恶意软件应用自动启动。用户需要至少打开一次该应用程序,然后它才能自动执行操作。

Meaning you will not receive ACTION_PACKAGE_INSTALLED, BOOT_COMPLETED etc. until the user manually launches the app. Google has taken this decision to prevent malware apps from auto-launching. The user needs to open that app at least once, for it to perform actions automatically after that.

希望我这次明确了。谢谢。

Hope I made it clear this time. Thanks.

这篇关于在Android中启动设备时,应用程序不会自动启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆