Android 10.0 Application startup on BOOT
Asked Answered
R

2

9

We have an android application which we intend to start/launch during phone boot. With some code tries in Android 10, we realized that the launching of app on boot, is not possible after Android 8.0. Previously in Android 6, it was possible. Even in physical device /phone / emulator Android 10 , we gave permission in AutoStart list our application. << Objective: Any way (workaround) to launch app on boot even on latest versions , i.e. Android 8 onwards ? >>

Tries that we made in Android 10: following are 3 sections of code - AndroidManifest.xml, MyActivity.java, MyBroadcastReceiver.java

1)AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<activity android:name=".MainActivity"  android:launchMode="singleTop" android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

2)MyActivity.java

public class MainActivity extends FlutterActivity {
@java.lang.Override
protected void onCreate(android.os.Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
// "Display pop up window"
    if (!Settings.canDrawOverlays(getApplicationContext())) {
        startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
    }
Log.d(TAG, "-------- onCreate -------"); // this is printed
}
}

3)MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
Log.d(TAG, "------ tried to launch MainActivity -------"); // this is printed
}
}
}
Rimple answered 2/11, 2020 at 8:48 Comment(8)
#60699744Cottontail
@Dan : Thanks for your suggestion. I edited the code : (a) added manifext SYSTEM_ALERT_WINDOW (b) in MyBrodcastReceiver.java :: onReceive() -- i edited -->> Intent i = new Intent(context, MainActivity.class); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(i); Log.d(TAG, "------ tried to launch MainActivity -------"); // this is printed in log...but app is not launched..Rimple
Can you show code? Edit your first post to include the changes to check them outCottontail
@Dan : I edited the code in above post. I test/deploy the application in my Redmi 6 pro MIUI Global 11.0.7.1, Android 9 PKQ1.180917.001. I even tried /tested in emulator Pixel 2 x86 Android 10.0+. same result, after boot, application does not launch. In Redmi 6 pro, application is even enlisted in AutoStart permission list.Rimple
Did you check for overlay permissions? Did you make sure you have given them to your app?Cottontail
@Dan : I now added code for providing overlay permission in MainActivity :: onCreate(), so , now in my Redmi6 pro, the permission screen pops (Display over other Apps) during deployment, but unfortunately, in my Redmi6 pro, after boot, this permission is not sufficient to launch after boot, So what i did, in my Redmi, I gave manually one extra permission under Settings>Apps>Permissions>Display pop-up windows while running in background. This makes it work now.Rimple
But in emulator, Pixel 2 (android 10.0), the permission Display over other apps is sufficient to work. So thank you a lot for identifying the issue and a resolution.Rimple
Sure thing, glad you got it to work :)Cottontail
S
4

If your app can get "SYSTEM_ALERT_WINDOW" permission (Draw Over Apps) the app wont get terminated after auto boot on Android 10 onwards.

So you need to display this settings activity and ask the user to enable it:

if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 0);
}

This is mentioned in the docs last bullet.

Sexist answered 23/7, 2022 at 19:19 Comment(0)
T
3

Starting the app on boot can be annoying to the user. You can start a Foreground Service instead. The BOOT_COMPLETE intent can be in many different form depending on your device. I have tried to capture all of them here.

In your Manifest:

<receiver android:name=".receiver.BootReceiver" // YOUR RECEIVER HERE
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>

And in the receiver:

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        // START FOREGROUND SERVICE HERE
    }
}
Transcript answered 2/11, 2020 at 9:38 Comment(2)
Basically, by service start during Boot in onReceive(), is done and tested. And we have a notification for that in notification drawer after booting gets completed. And when user taps on that notification, we are launching the application (this is done via PendingIndent()). This is okay and working. But our objective / requirement is -- in Android 8+ additionally, we need to launch the application during boot, not to be required to tap on notification to launch manually.Rimple
Hola @Shibendra Chakraborty espero que este bien, ¿pudo solucionar su problema? yo tengo el mismo inconveniente y no lo he podido solucionar, he hecho todo lo que he encontrado en STACKOVERFLOW y aun así cuando enciendo mi teléfono (android >= 10) la app no inicia. Me puede colaborar por favor. GraciasBerne

© 2022 - 2024 — McMap. All rights reserved.