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 (Mechanism that is suppose to be manually launch when app is first installed)
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) {
...........
}
2 个解决方案
#1
6
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.
从Android 3.1开始,新安装的应用程序始终处于“停止”状态,将其移出停止状态的唯一方法是至少手动启动应用程序的任何活动一次。
For your problem,
对于你的问题,
1) You need to design a mechanism where the user needs to first open any Activity of the app manually after installing the app.
1)您需要设计一种机制,用户需要在安装应用程序后首先手动打开应用程序的任何活动。
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.
2)之后,您的BootReceiver将正常工作,它将能够自动启动该应用程序的任何活动。你的实现绝对正确。
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.
我在我的一个应用程序中遇到了同样的问题,每次设备启动时我都试图打开一个Activity,但它不适用于新安装的应用程序。一旦用户手动打开应用程序至少一次,应用程序就会退出“已停止”状态,一切正常。
EDIT
编辑
1) Please ensure that the <uses-permission>
is a direct child of the <manifest>
tag.
1)请确保
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.
2)请确保指定android:installLocation =“internalOnly”,否则如果应用程序安装在SD卡中,您将不会收到任何Boot Complete操作。
3) As I explained before also.
3)正如我之前解释的那样。
From Android 3.1, all apps are put in the stopped state which is the same as when user Force Closes any app.
从Android 3.1开始,所有应用程序都处于停止状态,这与用户强制关闭任何应用程序时相同。
While in this state, the application will not run automatically for any reason, until and unless launched manually by the user from the launcher.
在此状态下,应用程序不会出于任何原因自动运行,除非用户从启动器手动启动。
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.
这意味着在用户手动启动应用程序之前,您不会收到ACTION_PACKAGE_INSTALLED,BOOT_COMPLETED等。谷歌已采取此决定防止恶意软件应用程序自动启动。用户需要至少打开一次该应用程序,以便在此之后自动执行操作。
Hope I made it clear this time. Thanks.
希望这次我明确表示。谢谢。
#2
2
A newly installed app is placed in a "stopped" state until the app is actually launched for the first time. In this state none of your components will be activated, so your BOOT_COMPLETED
receiver will not run. You need to include an activity and have it be opened by the user; you can always disable the Activity using PackageManager
later.
新安装的应用程序处于“已停止”状态,直到应用程序第一次实际启动。在这种状态下,您的所有组件都不会被激活,因此您的BOOT_COMPLETED接收器将无法运行。您需要包含一个活动并由用户打开;您可以随后使用PackageManager禁用Activity。
This behavior was introduced in Android 3.1, you can read about it in the release notes (under the heading "Launch controls on stopped applications").
此行为是在Android 3.1中引入的,您可以在发行说明中(在“已停止的应用程序上启动控件”标题下)阅读此内容。
#1
6
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.
从Android 3.1开始,新安装的应用程序始终处于“停止”状态,将其移出停止状态的唯一方法是至少手动启动应用程序的任何活动一次。
For your problem,
对于你的问题,
1) You need to design a mechanism where the user needs to first open any Activity of the app manually after installing the app.
1)您需要设计一种机制,用户需要在安装应用程序后首先手动打开应用程序的任何活动。
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.
2)之后,您的BootReceiver将正常工作,它将能够自动启动该应用程序的任何活动。你的实现绝对正确。
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.
我在我的一个应用程序中遇到了同样的问题,每次设备启动时我都试图打开一个Activity,但它不适用于新安装的应用程序。一旦用户手动打开应用程序至少一次,应用程序就会退出“已停止”状态,一切正常。
EDIT
编辑
1) Please ensure that the <uses-permission>
is a direct child of the <manifest>
tag.
1)请确保
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.
2)请确保指定android:installLocation =“internalOnly”,否则如果应用程序安装在SD卡中,您将不会收到任何Boot Complete操作。
3) As I explained before also.
3)正如我之前解释的那样。
From Android 3.1, all apps are put in the stopped state which is the same as when user Force Closes any app.
从Android 3.1开始,所有应用程序都处于停止状态,这与用户强制关闭任何应用程序时相同。
While in this state, the application will not run automatically for any reason, until and unless launched manually by the user from the launcher.
在此状态下,应用程序不会出于任何原因自动运行,除非用户从启动器手动启动。
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.
这意味着在用户手动启动应用程序之前,您不会收到ACTION_PACKAGE_INSTALLED,BOOT_COMPLETED等。谷歌已采取此决定防止恶意软件应用程序自动启动。用户需要至少打开一次该应用程序,以便在此之后自动执行操作。
Hope I made it clear this time. Thanks.
希望这次我明确表示。谢谢。
#2
2
A newly installed app is placed in a "stopped" state until the app is actually launched for the first time. In this state none of your components will be activated, so your BOOT_COMPLETED
receiver will not run. You need to include an activity and have it be opened by the user; you can always disable the Activity using PackageManager
later.
新安装的应用程序处于“已停止”状态,直到应用程序第一次实际启动。在这种状态下,您的所有组件都不会被激活,因此您的BOOT_COMPLETED接收器将无法运行。您需要包含一个活动并由用户打开;您可以随后使用PackageManager禁用Activity。
This behavior was introduced in Android 3.1, you can read about it in the release notes (under the heading "Launch controls on stopped applications").
此行为是在Android 3.1中引入的,您可以在发行说明中(在“已停止的应用程序上启动控件”标题下)阅读此内容。