Android Question [SOLVED] - Starting B4XPages from a service module

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

We have a B4XPages app that includes a service module that starts with #StartAtBoot: true. Because B4XPages doesn't start upon a phone reboot, we would like to start B4XPages from that module when the user reboots their phone.

Maybe something like this?

B4X:
#Region  Service Attributes
    #StartAtBoot: true 
#End Region

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)

    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)

    If B4XPages.IsInitialized = False Then
        'Code to start B4XPages goes here'
    End If

    InitializeWhatsNeeded
End Sub

We already tried this coding, but it gave an error indicating the B4XPages manager could not handle it when the code flow reach the highlighted line.

B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)

    If B4XPages.IsInitialized = False Then 
        StartActivity(Main)
        StartActivity(B4XPages.MainPage)
    End If

    Log("At service start.")

    InitializeWhatsNeeded
End Sub

We are assuming that this is the wrong approach even though using StartActivity was what we thought was the logical choice. We also couldn't find out by searching the forum how everyone else is starting B4XPages from their service module.

All help will be appreciated.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Try:
B4X:
Sub Service_Start (StartingIntent As Intent)

    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)

    If IsPaused(Main) Then
         StartActivity(Main)
    End If

    InitializeWhatsNeeded ' <--- I don't know what this routine does and then if this line should be written before the "If IsPaused..."
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
InitializeWhatsNeeded initializes several timers and those timers have CallSubDelayed statements that run sub routines that are in B4XMainPage as well as reference variables over there as well.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Starting just main doesn't seem to also start B4XMainPage. The timers aren't what's crashing the app, it's the calls in the to run a sub routine in B4XMainPage. Later today I will try moving that sub routine from B4XMainPage to the service module and really see if that's what's crashing B4XPages.GetManager.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I haven't "played" with StartAtBoot before.
The test I have done seems to work (attached).
It also writes fake files, for convenience, to see the order in which the various events are triggered.

As just said, I have never "played" with StartAtBoot and related problems, so if the attached test is not good for you, it would be better if Erel intervened.


EDIT: At least on my smartphone the app starts few seconds after the boot.
 

Attachments

  • StartAtBootTest.zip
    15.3 KB · Views: 273
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
There is no such thing. B4XPages is tied to an activity (usually the main activity).

You can "start" B4XPages by starting the main activity: StartActivity(Main)
Note that starting from Android 10 it requires a special permission.
I was using Android 10 to develop and test the app. Please give details or a link about the permission and how to request it please. Thanks so much.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
We were able to get around not being able to get the B4XManPage running by removing all references to variable and calls to sub routines in the service module. We now have the app running perfectly. Once we know how to implement the permission on starting B4XPages from a service module we may start using that permission. For now we will do all new coding making sure not to depend on sub routines and public variables on the B4XManPage. I will mark this thread as solved because of post number 7 which indicates a special permission is needed to wake up B4XPages.

Here's a sample of coding we did to get the app to work prior to knowing about the special permission.

B4X:
' Go here if the schedule 1 is not running. This ensures the schedule is
' not stopped again and again for each tick of the schedule timer.
'-----------------------------------------------------------------------
If blnSchedule1IsPlayingSong = True Then
                            
   blnSchedule1IsPlayingSong = False
   blnScheduleIsPlayingSong = False ' Flag used in the Main module.
   intSongNumberSelected = 0
   blnAsongIsPlaying = True ' Toggle to make the song stop.

   PlayCurrentSongLoadBitmap

   If B4XPages.IsInitialized Then
      If B4XPages.MainPage.strCurrentLayout = "SongList" Then
         CallSubDelayed(B4XPages.MainPage, "BuildTheSongListCustomListView")
      End If
   End If

   Log("Schedule For Song " & intSongNumber & " has ended.")
End If

The code checks if B4XPages is loaded up because the user loaded up the app. Only then will the coding check what's in a variable over there then call the sub routine over there.
 
Upvote 0
Top