Android Question Services - StartAtBoot: True

sortec.nick

Member
Licensed User
Longtime User
In my application, I have a class called SvcLocationTracking.bas with the following content:
B4X:
#StartAtBoot: True

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim GStrClass As String = "SvcLocationTracking"


    Dim GBlnLoudExceptions As Boolean = True


End Sub

Sub Service_Create
    Dim StrMethod As String = "Sub Service_Create"
    Try

    Catch
        If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
    End Try
End Sub

Sub Service_Start (StartingIntent As Intent)
    Dim StrMethod As String = "Sub Service_Start (StartingIntent As Intent)"
    Try

    Catch
        If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
    End Try
End Sub

Sub Service_Destroy
    Dim StrMethod As String = "Sub Service_Destroy"
    Try

    Catch
        If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
    End Try
End Sub

When I run the apk, the app runs fine.

Now, when I restart the phone, it powers down as normal and resumes as normal except for a message that says, "Unfortunately, UKSM PhoneApp has stopped."

The error log displays the following:
B4X:
java.lang.RuntimeException: Unable to create service sortec.uksm.btphoneapp.svclocationtracking: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException


   at android.app.ActivityThread.handleCreateService(ActivityThread.java:2681)
   at android.app.ActivityThread.access$1600(ActivityThread.java:156)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:153)
   at android.app.ActivityThread.main(ActivityThread.java:5297)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at sortec.uksm.btphoneapp.svclocationtracking.onCreate(svclocationtracking.java:33)


   at android.app.ActivityThread.handleCreateService(ActivityThread.java:2671)
   ... 10 more
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at sortec.uksm.btphoneapp.svclocationtracking.onCreate(svclocationtracking.java:31)
   ... 11 more

The only part of this that I recognise is svclocationtracking.java, which was what alerted me as I presume it is the java counterpart of my SvcLocationTracking.bas service class.

As you can see from my code, my standard practice for trapping exceptions are within each function/method, and so the 'Exception/Error spew' above means little to me.

However, the part that sticks out to me is an InvocationTargetException, which I found some information on here: http://developer.android.com/reference/java/lang/reflect/InvocationTargetException.html

The reason the service code is so empty is due to at first thinking that it was my code that was causing the malfunction, so I reduced it down as far as it would go and still get the error.

Changing #StartAtBoot: True to #StartAtBoot: False stops the error, but this is no good as I need the service to run on start-up.

Any help will be appreciated.
 
Last edited:

sortec.nick

Member
Licensed User
Longtime User
Are you sure that this is the full error message? Seems like it is missing an important part.

*Face palm* I'm so sorry. I appear to be incapable of copy & paste

The error message in full is:
B4X:
LogCat connected to: 410720f81ded31a9
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


java.lang.RuntimeException: Unable to create service sortec.uksm.btphoneapp.svclocationtracking: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException


    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2681)
    at android.app.ActivityThread.access$1600(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5297)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at sortec.uksm.btphoneapp.svclocationtracking.onCreate(svclocationtracking.java:33)


    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2671)
    ... 10 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at sortec.uksm.btphoneapp.svclocationtracking.onCreate(svclocationtracking.java:31)
    ... 11 more
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at sortec.uksm.btphoneapp.main.initializeProcessGlobals(main.java:443)
    ... 14 more
Caused by: java.lang.NullPointerException
    at sortec.uksm.btphoneapp.main._process_globals(main.java:478)
    at sortec.uksm.btphoneapp.main.initializeProcessGlobals(main.java:405)
    ... 14 more
 
Upvote 0

sortec.nick

Member
Licensed User
Longtime User
Something wrong in your Process Globals.
I am guessing you are trying to read or load a file/resource in there and the sdcard isnt ready.

Hmm, I presume you mean the Process Globals in my main activity. There is an SQLite object created there. Could that be it?

Here is my Main activity:
B4X:
#FullScreen: True

#IncludeTitle: True

#ApplicationLabel: UKSM PhoneApp

#VersionCode: 10

#VersionName: November2013

#SupportedOrientations: Portrait


'Activity module
Sub Process_Globals
    Dim GStrClass As String = "Main"
   
   
    Dim GBlnLoudExceptions As Boolean = True
   
    Dim GBlnToastMessageShow As Boolean = False
   
    Dim GBlnLoggingEnabled As Boolean = False
   
   
    Dim GSQLite As SQL
   
   
    Dim StrRootFolder As String = "/mnt/sdcard"
   
    Dim StrUKSMFolder As String = "uksm"
   
    Dim StrSQLiteFileLoc As String = StrRootFolder & "/" & StrUKSMFolder
   
    Dim StrSQLiteFileName As String = "uksm.sqlite"
       
    Dim StrInvoiceFolder As String = "InvoiceSigs"
   
    Dim StrWSURL As String = "http://xxx.xxxxxxx.xx.xx:xxxxx/system/a.ashx" 'Temporarilly blanked for posting into B4A forum
   
    Dim StrWSBURL As String = "http://xxx.xxxxxxx.xx.xx:xxxxx/system/b.ashx" 'Temporarilly blanked for posting into B4A forum
               
    Dim IntAppVersionNum As Int = CdSys.GetVersion
       
    Dim BlnExitApplication As Boolean = False
   
   
End Sub

Sub Globals
       
End Sub


#Region Lifecycle Overrides

    Sub Activity_Create(FirstTime As Boolean)
        Dim StrMethod As String = "Sub Activity_Create(FirstTime As Boolean)"
        Try
            BlnExitApplication = False
           
            StartService(SvcLocationTracking)
       
        Catch
            If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
        End Try
    End Sub

    Sub Activity_Resume
        Dim StrMethod As String = "Sub Activity_Resume"
        Try
            If BlnExitApplication Then
                ExitApplication
               
            Else
                File.MakeDir(StrRootFolder, StrUKSMFolder)
               
                If Not(GSQLite.IsInitialized) Then               
                    GSQLite.Initialize(StrSQLiteFileLoc, StrSQLiteFileName, True) 'True creates the database if it's not there.
                   
                End If
               
                'Perform schema checks.
                Dim ClsDBCheck As ClsDatabaseBuilder
                ClsDBCheck.Initialize
               
                ClsDBCheck.BuildSchema
               
                CdTabletSetting.WipeEngineer
               
                StartActivity(FrmSystemSetup)
           
            End If
       
        Catch
            If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
        End Try
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

#End Region
 
Upvote 0

sortec.nick

Member
Licensed User
Longtime User
I dont see any problems in the code you posted so far.
Could this line return a nullpointer?
Dim IntAppVersionNum AsInt = CdSys.GetVersion

The code for that function is:
B4X:
Sub GetVersion As Int
    Dim StrMethod As String = "Sub GetVersion As Int"
    Dim IntResult As Int = 0
    Try
        Dim PackageManager As PackageManager
       
        Dim IntAppVersionNum As Int
        IntAppVersionNum = PackageManager.GetVersionCode("sortec.uksm.btphoneapp")
       
        IntResult = IntAppVersionNum
       
    Catch
            If GBlnLoudExceptions Then LogColor("Exception: " & LastException.Message & " - Class: " & GStrClass & " - Method: " & StrMethod, Colors.Magenta)
    End Try
    Return IntResult
End Sub
 
Upvote 0
Top