Database record via a service

markh2011

Member
Licensed User
Longtime User
Hi, i am probably doing this wrong but i am trying to add a record to a database from within a service. Is this possible?

B4X:
'Service module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim GPS1 As GPS
    Dim count As Int
    Dim edtLat, edtLong As Float

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
End Sub

Sub Service_Create
GPS1.Initialize("GPS")
 If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    End If
End Sub

Sub GPS_LocationChanged (Location1 As Location)
   count = count + 1
    ToastMessageShow("GPS Data" & Location1.Latitude & " " & Location1.Longitude, True)
    addrec
   GPS1.Stop 
End Sub

Sub Service_Start (StartingIntent As Intent)
      StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerMinute, False)
      ToastMessageShow("Turning GPS ON", True)
     GPS1.Start(0, 0) 'Listen to GPS with no filters.
      count = 0
End Sub

Sub Service_Destroy

End Sub

Sub addrec
    dbutils.CopyDBFromAssets("findem2.db")
   Dim DBFileName As String                              : DBFileName = "findem2.db"
   Dim DBFilePath As String                              : DBFilePath = File.DirDefaultExternal
   Dim DBTableName As String                              : DBTableName = "location"
   Dim a, b As EditText
   Dim sql1 As SQL
   a.Initialize("")
   b.Initialize("")
   a.Text= edtLat
   b.Text= edtLong
   sql1.ExecNonQuery2("INSERT INTO location VALUES(?, ?)",Array As Object(a.text, b.text))    
End Sub
Any help will be appreciated
 
Last edited by a moderator:

markh2011

Member
Licensed User
Longtime User
Hi Errol,
I have attached my code to this post.

After lots of frustration at my inability to get it working i have reformatted the code with a little bit of success....but not enough.

the text fields were accidentally left behind in the code
When i removed the accrec from the service it would run as expected & show the data on the screen [lat & long]. When i used the accrun method in the service it would crash each time.

I have now moved this method in to the Activity and call it in the service.
It runs ok but only creates a single record in the dbase & every time the service runs it does not add the next record.
i was putting the text fields in the activity to try to get some values to appear in a screen for just my learning....

In the end product i will not be showing any data in messages to the person who is being tracked.
 

Attachments

  • spot.zip
    11.4 KB · Views: 143
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Service_Create
GPS1.Initialize("GPS")
 If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    End If
End Sub

Sub GPS_LocationChanged (Location1 As Location)
   count = count + 1
    ToastMessageShow("GPS Data" & Location1.Latitude & " " & Location1.Longitude, True)
'    addrec
   edtLat = Location1.Latitude
   edtLong = Location1.Longitude
   GPS1.Stop 
End Sub

Sub Service_Start (StartingIntent As Intent)
      StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerMinute, False)
      ToastMessageShow("Turning GPS ON", True)
     GPS1.Start(0, 0) 'Listen to GPS with no filters.
     count = 0
     CallSub(Main, "addrec")     
End Sub

Sub Service_Destroy

End Sub
Service_Start is executed right after Service_Create. This means that the GPS coordinates will not be ready yet.

Some tips:
1. You should only initialize the SQL object when FirstTime is true.
2. Calling addrec in the activity will only work when the activity is active. You should add the record from the database.
3. Eventually your service will be killed. You should either call Service.StartForeground or schedule the service with StartServiceAt.
 
Upvote 0

markh2011

Member
Licensed User
Longtime User
Hi Erel,
I am still stuck with where to call the createRecord. I dont really understand what i need to do to get it working

I have rewritten the code & i think i have followed your tips but no matter where i call the createRecord the application immediately fails & i get a force close message.
The gps coordinates seem to be generating as expected when i remove the call to create the database record...i just dont know where to call the method from.
Could you please have a look at the updated code & show me exactly where/what i need to do.
Sorry for being a dumbo noob...
 

Attachments

  • spot2.zip
    11.4 KB · Views: 164
Last edited:
Upvote 0
Top