Android Question [SOLVED] - B4XPages - Starting service module when phone reboots

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I have this code to start a service module when the phone reboots but it doesn't start because my notification is not displayed on the app icon on the phone home screen. This was working in my original app before migrating it to B4APages. Is there any additional coding I need to add so the service module will start upon rebooting the phone? The good news is that Android doesn't kill this app and it runs in the background perfectly. I could tell the user to load the app on reboot but would rather it start by itself. :)

Attribute to start the service module when the phone reboots.:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

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

Sub Service_Start (StartingIntent As Intent)
   
    Log("Starting service")
   
    Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
   
    InitializeWhatsNeeded
   
    tmrForSchedules.Enabled = True
End Sub

Code for the notification.:
Sub CreateNotification (Body As String) As Notification

    Dim notification As Notification

    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Now running in the background.", Body, Main)

    Return notification
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Can you tell why the service module is not starring up if the user reboots?

The difference between this app and the original was I also had the StartAtBoot attribute in the Starter module as well as the service module.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The difference between this app and the original was I also had the StartAtBoot attribute in the Starter module as well as the service module.
You should NOT set StartAtBoot in the Starter Service!
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I maDe a test app and couldn't get it to reload on reboot until I moved the call to start the service module from the B4XMainPage module to the Main module. I'm providing a link to the test app for anyone who got stuck in the same way. My current app still doesn't start on reboot so I'm going to slowly add parts of the app to the small test app until I hit the coding that's causing it not to restart on reboot.

Because the small test app works. I will mark this thread as solved.

Thanks for suggesting that I should make a small test app for testing.

Here's the test app.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I moved the call to start the service module from the B4XMainPage module to the Main module
Out of curiosity, I ran your service app, although I do not need any of my apps with services. I moved back the line of code to the B4XMainPage as such:
B4X:
Sub B4XPage_Appear
    StartService(ServiceModule)
End Sub
I rebooted the tablet few times and it ran for me. I set the time interval to 2 minutes to speed up the process. Two minutes after the reboot, the service restarted fine. My knowledge in B4XPages is still shaky like many of us still, but that is what I observed.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Out of curiosity, I ran your service app, although I do not need any of my apps with services. I moved back the line of code to the B4XMainPage as such:
B4X:
Sub B4XPage_Appear
    StartService(ServiceModule)
End Sub
I rebooted the tablet few times and it ran for me. I set the time interval to 2 minutes to speed up the process. Two minutes after the reboot, the service restarted fine. My knowledge in B4XPages is still shaky like many of us still, but that is what I observed.
I guess I didn't wait long enough for the phone to complete the reboot process. The small app starts the service module even with the coding in B4XMainPage and also in Main so I will copy that small module, rename the app name and start coping everything slowly into the copy and see what coding in my app was not causing the service module to load. I have a sneaky suspicion that maybe the service module loads before the B4XMainPage. In the service module I do a CallSubDelayed to the B4xMainPage. Maybe that is messing something up. I will know more when I start putting code back into the copy of that app. When I where the problem was, I will let everyone know what caused it so others won't get stuck like that.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I found what was actually happening so I will take out the "Solved" from the thread.

When I run the app by tapping on the app icon, the B4XMainPage loads before the service module which in turn starts the service module. At that point the app works as it should.

When I reboot the phone, the service module loads up first which crashes the app because I think it tries to load the B4XMainPage because of the b4xpages_getmanager error which shows it's failing to load it somehow.

I have a couple of log statements in my coding. One is at B4XPage_Created and the other is in the service module which you can see in the logs. The log statement in the B4XMainPage never is shown because that's when the app crashes.

Is there a way to stay in the service module and wait until the B4XMainPage has loaded, then have the program flow continue onwards so the app won't crash? Maybe there's a way to have the service module check if it was started as a result of rebooting the phone? If that can be done, I can add code that only executes the stuff that just keeps the service module running then execute the rest of the stuff after it detects the B4XMainPage is up and running.

B4X:
** Service (starter) Start **
** Service (servicemodule) Create **
** Service (servicemodule) Start **
Service started in foreground mode.
At service start.
b4xpages_getmanager (java line: 54)
java.lang.ClassCastException: java.lang.Object cannot be cast to b4a.natures.song.b4xpagesmanager
    at b4a.natures.song.b4xpages._getmanager(b4xpages.java:54)
    at b4a.natures.song.b4xpages._mainpage(b4xpages.java:98)
    at b4a.natures.song.servicemodule._tmrforschedules_tick(servicemodule.java:593)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:8125)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
No more app crash! That one really had our team stumped.

Here's the coding to get it up and running.

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)

    ' Wait for B4XPages to catch up with this service module.
    '--------------------------------------------------------
    Do Until B4XPages.IsInitialized       
    Loop

    Log("At service start.")

    InitializeWhatsNeeded
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I changed it a bit so there's no loop and I tried to start the both main activity and the B4XMainPage activity. I discovered I can't start both like that because the app will crash. I found I could start the Main activity but don't know how I can get the B4XMainPage activity. The service module has CallSubDelayed statements and variable references in the B4XMainPage so we need that activity to run with the service module if the user reboots their phone.

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

        Log("Starting Main & Main Page activities.")
     
        StartActivity(Main)
        StartActivity(B4XPages.MainPage)
    End If

    Log("At service start.")

    InitializeWhatsNeeded
End Sub
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I'm closing the issue because the original objective was to start our service module when the phone reboots.
 
Upvote 0
Top