Android Tutorial Service Modules

schimanski

Well-Known Member
Licensed User
Longtime User
I'm working with two services in my application and it runs very fine, but I ask me, if it is better to work only with one service in my application. It is possible to start one service with Activity create and let the service do all the things, which have to run in the backround. But when I do this, it is nessesary to create different timers in the service. I don't know, what the better programming is.

Rgds
 

LittleEddie

Member
Licensed User
Longtime User
I'm wanting to write a service that has no Front end,

It is called from an Intent in another App with data and does things to the data and then returns data back to the calling App.

I've only been studing this the last half hour and I think it's doable but I thought I'd ask to save some time.

Ed
 

jndaniels1

Member
Licensed User
Longtime User
another service question

I've read the help program; very good reference. Most things I've delt with were activity processes. Now I am wondering about services and threads.
would it be better to run some GPS, TTS, and Voice Recognition as a thread or a service? Being a service i can throw a notice up.

Just a general question; wanting to know which why to approach my next project..

Much appreciate it!
Cheers
.JND.
 

anaylor01

Well-Known Member
Licensed User
Longtime User
I am trying to do exactly like the example but everytime I click the back or home key the app stops running. What am I doing wrong?
 

Taha

Member
Licensed User
Longtime User
It is strange - I made a notification, it appears and works correct but when I press on the notification - the application doesn't open.

 

FHEBERT

Member
Licensed User
Longtime User
Hi,
I've a problem when my phone is standby. The timer stopped.
You've a idea ?
My timer is declared in my process globals of the module
thanks for your help
 

BarrySumpter

Active Member
Licensed User
Longtime User
How hard would it be to add the code to make the GPS.B4A a background service?

I've had a good study of the Twitter app but can't work out which is the twitter script and which is the background service script.

Perhaps a Twitter.b4a WITHOUT the server script.
Then I could compare the two.

Any help would be greatly appreciated.

myGPStxt.b4a
B4X:
Sub Process_Globals
   Dim GPS1 As GPS
End Sub

Sub Globals
   
   Dim lblLon As Label
   Dim lblLat As Label
   Dim lblSpeed As Label
   Dim lblSatellites As Label
   Dim lblDateTimeStamp As Label
   
  Dim Map1 As Map
     
End Sub

Sub Activity_Create(FirstTime As Boolean)

   If FirstTime Then
      GPS1.Initialize("GPS")
  End If
   
'   If File.ExternalWritable = False Then
'      Msgbox("Cannot write on storage card.", "")
''      Return
'  End If

   
   Activity.LoadLayout("1")


End Sub

Sub Activity_Resume
   If GPS1.GPSEnabled = False Then
      ToastMessageShow("Please enable the GPS device.", True)
      StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
   Else
      GPS1.Start(0, 0) 'Listen to GPS with no filters.
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)

  Dim dt As String
   Dim now As Long
  
   DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
   
   now = DateTime.now
   dt = DateTime.Date(now)

        ' Google Maps API calls are in Seconds not minutes
   lblLat.Text = "Lat = " & Location1.ConvertToSeconds(Location1.Latitude)
   lblLon.Text = "Lon = " & Location1.ConvertToSeconds(Location1.Longitude)
   lblSpeed.Text = "Speed = " & Location1.Speed
   lblDateTimeStamp.text = dt

   Map1.Initialize
        ' Google Maps API calls are in Seconds not minutes
   Map1.Put ("Lat", Location1.ConvertToSeconds(Location1.Latitude))
   Map1.Put ("Lon", Location1.ConvertToSeconds(Location1.Longitude))
   Map1.Put ("Speed", Location1.Speed)
   Map1.Put ("DateTimeStamp", dt)
   File.WriteMap(File.DirRootExternal, "GPS.txt", Map1)

End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
   ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
   lblSatellites.Text = "Satellites:" & CRLF
   For i = 0 To Satellites.Size - 1
      Dim Satellite As GPSSatellite
      Satellite = Satellites.Get(i)
      lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
         " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
         & " " & Satellite.Elevation
   Next
End Sub
 
Last edited:

BarrySumpter

Active Member
Licensed User
Longtime User
Hi Erel,
Just wanted to bump this request of updating this simple GPS code to be a service to run in the background.
My research indicates that it should only take about 12 to 15 lines of script.
I've had a coupe of goes but can't tell where to put what.
I feel this would be a fantastic way to show how easy it is to convert a b4a app into a service.
As soon as I can see a working service before and after script I'll be able to convert future b4a apps on my own.
I know you're busy and if I'm asking too much just let me know and I'll understand.

If anyone else can assist I'd certaily appreciate any help.

All my best to all.
 

BarrySumpter

Active Member
Licensed User
Longtime User
I've already spent a couple hours to-ing and fro-ing trying to get it to work. :sign0104: :BangHead:
So confused n agro I deleted all my work to start again.
And ran out of energy.
:sign0013:

What a shame.
A great opportunity to learn something from the master. :sign0188:
Missed. :sign0148:

Now I'm thinking if its so easy to do why hasn't someone else posted a solution. :sign0163:
So I'm feeling a bit better about not being able to do it.

I've already posted the GPS script to start from.
I think you could have used that 2 mins to reply toward the 5 mins it would take to make this a service. :sign0188:
Missed.:sign0148:

I've moved back to LiveCode.
They seem to have sorted out their GPS functionality in the latest version releasaed a few days ago.
I was so looking forward to showing off the GPS service that b4a could deliver.
And LC couldn't.
Anoher missed opportunity.:sign0148:

:sign0163:
:sign0163: at all?
:sign0085:
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User

rfresh

Well-Known Member
Licensed User
Longtime User
I was wondering if using FTP in a Service does anything funny to the StartServiceAt() statement?

If I'm not downloading or uploading a file, the line

B4X:
StartServiceAt("",DateTime.Now + 20 * 1000,True)

works fine and the service module cycles every 20 seconds.

However, if I throw in a file download or upload, it stops the service cycle (the download and upload is successful). Is there anything I have to do special in a service module when using FTP?

Thanks...
 

rfresh

Well-Known Member
Licensed User
Longtime User
I need to download the file every so many minutes. It's a very small file and I'm set to download it every 10 mins.

Do I need to stop the service module interval just before downloading and then make the StartServiceAt() call again when it's done downloading?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…