Android Question Running an App at startup

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys
I trying to get a App to run when my phone is restarted (i.e. boots). I have tried code in a previous post here post #3 (test.zip). it dates from 2018 but last post #7 suggests it works - It does not work for me after I install the App and reboot the phone. However, the closing comment also states .

"On my os there is a setting under Apps , click on app for settings , change Restrict to launch = off ....
all working ..."

I can't find this option in the App setting on my phone, unless I have misunderstood where to look. Android help discusses Restricted Settings Here but I still can't find them on my phone.

Can someone help

Dave
 

JohnC

Expert
Licensed User
Longtime User
The first thing to do is add the below line in the Service_Start sub of the example project you linked to:

B4X:
Sub Service_Start (StartingIntent As Intent)
   
    objectTimer.Initialize ("timer", 2000)
    objectTimer.Enabled = True
   
    ToastMessageShow("Bootup Service Started",True)  '<=== add this line

End Sub

Then reboot your device and look for a quick pop-up message that says "Bootup Service Started"

It may take a few minutes to display that message depending on your device.

If you see the message, then that's a good start - so post an update of what you found.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi, Guys
I trying to get a App to run when my phone is restarted (i.e. boots). I have tried code in a previous post here post #3 (test.zip). it dates from 2018 but last post #7 suggests it works - It does not work for me after I install the App and reboot the phone. However, the closing comment also states .

"On my os there is a setting under Apps , click on app for settings , change Restrict to launch = off ....
all working ..."

I can't find this option in the App setting on my phone, unless I have misunderstood where to look. Android help discusses Restricted Settings Here but I still can't find them on my phone.

Can someone help

Dave
You really should be using a Broadcast Receiver. Have a look here -> https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/

- Colin.
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi, John
I put the toast message int the AutoStart service an you suggested - and reinstalled.
After the phone was rebooted, about a minute later the toast message appeared ("Bootup Service Started")

However, the app did not appear on the screen.

I went to Settings->System->Developer Options->Running Services and find the App (B4A.Example) was running with 2 services.

Also the App was listed when I pulled down notifications in the list - when I click on that notification the App showed on the phone.

Regards
Dave
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, what version of android is running on your device?

I believe from Android 8.0 or above a service can no longer directly display an activity. It can display a notification, which can then display an activity.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Its Android 10 - Patch update 1 Aug 2021

Regards
Dave
Yeah, that's probably the problem.

The only thing you can do is have the service display a notification, which the user can then display an activity from it.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
the broadcast receiver posts relating for 2023+ may not
work quite the same way on android 10. my comments
concern android 12+:

assuming you can get the receiver to run at boot start
(actually, it runs at boot completed), you have the problem
of getting it to run your app. having the receiver, eg,
check the weather is one thing, launching your app is
another. you need the so-called "draw over" permission
(system window permission in google parlance).

this is a funny permission. it is presented (by google) as
a runtime permission, but it won't work like one in b4a.
you will need to send the user to the settings screen where
they will manually permit draw over (as if the average user
actually has any idea of what that means.)

running on android 12 pixel 3a, that's the way it works. when
i turn the device on, it goes through the boot process (very long)
and broadcasts to the receiver that it's up and running. the
receiver launches main.

after you deploy the app to the device, you go to the app settings
screen and enable "draw over". you can access settings
programmatically, so you can probably have your app show the
user the settings screen. i've done that for other apps. this was
just proof of concept to see if the new receivers worked as expected.

starting a service at boot no longer works. it may have worked for
android 10.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi, Colin

If I use a broadcast receiver will it display the App - (I think I have been spoilt with Windows - startup menu and expecting a simple solution in Android) - If it does I need to do some investigation.

Regards
Dave
I was just about to reply, but @drgottjr beat me to it. I was going to say pretty much the same thing... :)

- Colin.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
I was just about to reply, but @drgottjr beat me to it. I was going to say pretty much the same thing... :)

- Colin.
brilliant minds - even with parts missing - think alike.

just a quick follow-up. here's what google has to say about getting the user to approve "overlay":
"To prompt the user to grant this approval, the app must send an intent with the action ACTION_MANAGE_OVERLAY_PERMISSION, which causes the system to display a permission management screen."
so, it is the same as before if you wanted to send the user to a systems setting dialog. when you deploy the app to the device and run it for the first time, you can check to see if it has overlay permission (which, obviously, it won't). then you would run the intent. since it is not a runtime permission, there is no usual runtime permission dialog. you'll probably have to invoke a quick javaobject one-liner to see if user has already
granted permission (Settings() public static boolean canDrawOverlays (Context context)).

quick follow-up 2: i just tried it using an intent. it's always klunky ending up at the settings screen, but that's what google wants, and it works as expected. i followed through, re-booted, and the receiver launched the app without further hassle.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
brilliant minds - even with parts missing - think alike.

just a quick follow-up. here's what google has to say about getting the user to approve "overlay":
"To prompt the user to grant this approval, the app must send an intent with the action ACTION_MANAGE_OVERLAY_PERMISSION, which causes the system to display a permission management screen."
so, it is the same as before if you wanted to send the user to a systems setting dialog. when you deploy the app to the device and run it for the first time, you can check to see if it has overlay permission (which, obviously, it won't). then you would run the intent. since it is not a runtime permission, there is no usual runtime permission dialog. you'll probably have to invoke a quick javaobject one-liner to see if user has already
granted permission (Settings() public static boolean canDrawOverlays (Context context)).

quick follow-up 2: i just tried it using an intent. it's always klunky ending up at the settings screen, but that's what google wants, and it works as expected. i followed through, re-booted, and the receiver launched the app without further hassle.
This is what it looks like in Kotlin (not much use in B4A I know, but it might at least give you an idea of how it's implemented. This is from an app I wrote that would launch a service when the device was rebooted.):
C-like:
//startActivityForResult is deprecated in Android, so we need to use Activity Result Contracts instead. I'm not sure how this is implemented in B4A, or if you still use startActivityForResult & Erel has changed it in the background.

private val overlaySettingsResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            binding.chkStartAtBoot.isChecked = false
            saveBootPref(false)
        }
    }
}

@RequiresApi(Build.VERSION_CODES.M)
private fun checkOverlayPermission() {
    if (!Settings.canDrawOverlays(this)) {
        AlertDialog.Builder(this)
            .setTitle(R.string.start_at_boot)
            .setMessage(R.string.boot_start_explain)
            .setPositiveButton(R.string.do_continue) { _, _ ->
                val i = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:${applicationContext.packageName}"))
                overlaySettingsResult.launch(i)
            }
            .setNegativeButton(android.R.string.cancel) { _, _ ->
                binding.chkStartAtBoot.isChecked = false
                saveBootPref(false)
            }
            .create().show()
    }
}

- Colin.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
This is what it looks like in Kotlin (not much use in B4A I know, but it might at least give you an idea of how it's implemented. This is from an app I wrote that would launch a service when the device was rebooted.):
C-like:
//startActivityForResult is deprecated in Android, so we need to use Activity Result Contracts instead. I'm not sure how this is implemented in B4A, or if you still use startActivityForResult & Erel has changed it in the background.

private val overlaySettingsResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            binding.chkStartAtBoot.isChecked = false
            saveBootPref(false)
        }
    }
}

@RequiresApi(Build.VERSION_CODES.M)
private fun checkOverlayPermission() {
    if (!Settings.canDrawOverlays(this)) {
        AlertDialog.Builder(this)
            .setTitle(R.string.start_at_boot)
            .setMessage(R.string.boot_start_explain)
            .setPositiveButton(R.string.do_continue) { _, _ ->
                val i = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:${applicationContext.packageName}"))
                overlaySettingsResult.launch(i)
            }
            .setNegativeButton(android.R.string.cancel) { _, _ ->
                binding.chkStartAtBoot.isChecked = false
                saveBootPref(false)
            }
            .create().show()
    }
}

- Colin.
inline kotlin - the next frontier!
it's pretty much the same story in b4a. a few differenet ways to implement; here's one:

B4X:
Sub Activity_Resume
    Dim jo As JavaObject
    jo.InitializeContext
    Dim settings As JavaObject
    
    If Not(settings.InitializeNewInstance("android.provider.Settings",Null).RunMethod("canDrawOverlays",Array(jo))) Then
        If Not(triedAlready) Then
            Dim intent As Intent
            intent.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION","")
            Try
                triedAlready = True   ' process global, if you're looking for it
                StartActivity( intent )
            Catch
                Log("Uh oh; we can't even get to the drawover settings screen.  receiver will run, but i won't be launched.")
                Activity.Finish
            End Try
        Else
            Log("sorry, permission denied.  receiver will run, but i won't be launched.")
            Activity.Finish
        End If
    Else
        Log("we're good to go.  re-boot and i'll be launched.")
    End If
    
    
End Sub
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Hi Guys

Thanks for the information there is a lot to take in - did try my original code (referenced in post #1) on an Android 5 tablet and it worked - did not try on Android 6 - 9 but considering the comments above it should work but for Android 10+ you will have problems.

I only wanted to start an app running on an Android TV box (running Android TV 13) which displayed information and again found the original code it did run start boot up.

However, it appears I am not the only one with this problem because I found some "Launch on Boot" Apps in the Google play store. So I installed Boot App (which as free) and it runs the App at startup automatically.

However, you do have to set the permissions , the app does guide you through it - so it's not to bad. It also helps to make sense of what Colin (Computersmith64) and drgottjr where taking about regarding permissions.

Thanks for the heads ups.

Kind regards
Dave


Report

.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I only wanted to start an app running on an Android TV box (running Android TV 13) which displayed information and again found the original code it did run start boot up.
Is this the only app that's supposed to be running on Android TV and no other apps will be used on Android TV?
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
Is this the only app that's supposed to be running on Android TV and no other apps will be used on Android TV?
Hi Peter
Currently, I intend to only run one app on the TV but don't know about the future. Also, I might add in the "Launch on boot" App setup it appears to allow several to be selected . Not really sure, until I try it, how multiple app are shown at startup unless of course they are intended to run in background i.e. a tracking App.

All the best
Dave
 
Last edited:
Upvote 0
Top