2

I am trying to get my Android app to start automatically on boot up. I have followed various examples and copied the code, but still, when I power up, the app does NOT start.

I am using a Galaxy Tab A7 Lite, running Android 11.

Any help gladly received.

Thank you.

Here is my code...

I have defined the receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartOnBootupReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            context.startActivity(activityIntent);
        }
    }
}

And in the AndroidManifest.xml file:

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

and

<receiver android:name=".StartOnBootupReceiver"
    android:exported="false"
    >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action..QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>
garrettb
  • 139
  • 11
  • note that "android.intent.action..QUICKBOOT_POWERON" has two "." instead of one –  Jul 14 '22 at 00:00

4 Answers4

1

Your code looks good and your receiver is most likely executing. However, it is crashing because of the call to startActivity(). Since Android 10, startActivity() can only be called if the application is already in the Foreground or if it meets one of the listed exceptions listed in the Android guide Restrictions on starting activities from the background.

As an aside, the value of the android:exported has no effect. The OS can still call receivers who have android:exported="false" and, in the case of exported="true", other application cannot send a broadcast with BOOT_COMPLETED or QUICKBOOT_COMPLETED because they are protected intent actions.

  • Thanks for your feedback. I understand why they would want to restrict this. However, what I want to do with my app is as follows: Use a Samsung tablet as a user interface to a machine. The tablet would be fitted to the body of the machine etc. So when the tablet power sup, I want it to launch the app automatically etc. I also want to try and hide the bottom navigation bar permanently. All this because the display I used to use for this machine is no longer available, because some of the chips are no longer available, so I thought that using a tablet might solve the problem. – garrettb Jul 14 '22 at 01:39
  • @garrettb In which case you want to look into making your app the Device Owner or use the [Android Management API](https://developers.google.com/android/management). This is one of the exceptions listed and will allow to have much more control over the device. Specifically, I believe you are looking for [Kiosk Mode](https://developers.google.com/android/management/policies/dedicated-devices#kiosk_mode). –  Jul 14 '22 at 04:09
  • Excellent - thank you for that feedback - I guess that's all I really need to know for now - and I will go and do my research. Thank you :) – garrettb Jul 14 '22 at 04:16
1

Starting with Android 10, the startActivity method only works if it is called by an application that is in the foreground. So you can request the following permissions:

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

Now request the REQUEST_OVERLAY_PERMISSION if android version is above 10:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Settings.canDrawOverlays(this)) {
      val intent = Intent(
          Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
          Uri.parse("package:$packageName")
      )
      startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION)
}

Once the permission is granted you can again use the startActivity method from your BroadcastReceiver.

alSan
  • 11
  • 2
  • This did the trick for me under Android 13, the only additional thing I had to do was add FLAG_ACTIVITY_NEW_TASK to the intent. – PeterJ Aug 16 '23 at 03:49
0

All your code is correct. However, if you look up android:exported="false" meaning you will find that:

The "exported" attribute describes whether or not someone else can be allowed to use it. So if you have "exported=false" on an Activity, no other app, or even the Android system itself, can launch it. Only you can do that, from inside your own application.

In your <receiver> statment, the android:exported="false" needs to be true.

That info borrowed from here and here.

Ars_Codicis
  • 440
  • 1
  • 4
  • 16
  • Thanks for your feedback Omeage_Pixel. However, I had already tried the above code with the 'exported=true', and indeed I have just re-tried that. However, the app still does not start after I restart the tablet. – garrettb Jul 13 '22 at 21:06
  • By the way - I wonder am I misunderstanding something. Essentially what I want is for my app to launch automatically after the tablet powers up - which is why I am trying the above code etc. – garrettb Jul 13 '22 at 21:07
  • It is doable, there are a bunch of posts all over the internet on how to do that... I'm assuming that you've already scoured the web, but this post may be of help https://stackoverflow.com/questions/6391902/how-do-i-start-my-app-when-the-phone-starts-on-android. Apparently, android 10 introduced a permissions requirement. – Ars_Codicis Jul 13 '22 at 21:15
  • Yes, I've been through those examples, still no luck. If anyone else reads this comment, I am still looking for a solution :) – garrettb Jul 13 '22 at 21:42
0

I had the same problem. The problem was solved when I got the following permission.

private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.canDrawOverlays(this)) {
        val intent = Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + this.packageName)
        )
        startActivityForResult(intent, 232)
    } else {
        //Permission Granted-System will work
    }
}

}