5

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> 
1
  • note that "android.intent.action..QUICKBOOT_POWERON" has two "." instead of one Commented Jul 14, 2022 at 0:00

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

3 Comments

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 In which case you want to look into making your app the Device Owner or use the Android Management API. 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.
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 :)
1

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.

5 Comments

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.
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.
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 stackoverflow.com/questions/6391902/…. Apparently, android 10 introduced a permissions requirement.
Yes, I've been through those examples, still no luck. If anyone else reads this comment, I am still looking for a solution :)
Hi garrettb I hope you are okay, ¿do you solve your problem? I have the same problem, but I haven't been able to solve it. Can you help me please
1

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 } } 

}

Comments

-1

We also had the same issue. Finally, I was able to solve it and describe it in detail in the blog Android Auto Launch. It has step-by-step information needed to solve it and a link to GitHub, where you can copy and paste code and run or copy it to your own project.

Pay attention to permission RECEIVE_BOOT_COMPLETED

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.