问题
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
<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
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)
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) {
...........
}
回答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.
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.
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.
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.
EDIT
1) Please ensure that the <uses-permission>
is a direct child of the <manifest>
tag.
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) As I explained before also.

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.
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.
回答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.
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").
来源:https://stackoverflow.com/questions/31353411/app-doesnt-auto-start-an-app-when-booting-the-device-in-android