Android Question Start at boot a foreground service

Maciej

Member
Licensed User
Longtime User
Hello,

I’m trying to start at boot a foreground service. I’ve double checked everything:

1.Service Attribute #StartAtBoot is set to True

2.Project Attribute #CanInstallToExternalStorage is set to Fase

But the service is not started on boot (Android 4.2.2)

Am I missing something ?

BR,

Maciej
 

Maciej

Member
Licensed User
Longtime User
In Sub Service_Create I create the file and write into it.
Actually the file is not created.
I also raise a notification which is not displayed.
 
Upvote 0

tunderin

Member
Licensed User
Longtime User
In Sub Service_Create I create the file and write into it.
Actually the file is not created.
I also raise a notification which is not displayed.


It could be that you are attempting your write operation before memory is fully initialized. Below is a sub that I wrote for a situation similar to yours. Don't attempt your write until it returns mounted

B4X:
'returns state of DirDefaultExternal - unmounted, mounted or shared
Public Sub myStorageState As String
    Dim r As Reflector   
    Dim ExternalStorageState As String=r.RunStaticMethod("android.os.Environment", "getExternalStorageState",Null,Null)
    Log("[myStorageState] -- Storage state is:  " &ExternalStorageState)
   
    Return ExternalStorageState
   
End Sub
 
Upvote 0

Maciej

Member
Licensed User
Longtime User

I also raise a notification wich is not displayed ...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It seems that launching the app once before rebooting solves the issue. I wonder why ?

Things like StartAtBoot becomes ACTIVE when you first start the App after Installing. Just installing the app does not make the StartAtBoot active. The App must be started at least once.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
check that fast boot is disabled if such a feature exist's on your device. I had the same issue on a HTC on X. I would recommend a timed delay of about 20 seconds in the boot service before you initialize your storage to allow storage to be initialised.
 
Upvote 0

tunderin

Member
Licensed User
Longtime User

Agreed. This is why I check memory init status on boot with the sub above. It seems that otherwise, app code begins to execute before external data memory is available for file operations.
 
Upvote 0

Similar Threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…