Android Tutorial Service Modules

Discussion in 'Tutorials & Examples' started by Erel, Jan 27, 2011.

  1. schimanski

    schimanski Well-Known Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    There is no problem with having multiple services. Whether you should use one or more depends on the tasks of each service.
     
  3. LittleEddie

    LittleEddie Member Licensed 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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    It will be much easier to communicate with a service that is local to your application.
     
  5. jndaniels1

    jndaniels1 Member Licensed 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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Services run in the same process (and same thread) as the activities.

    If there are features which shouldn't be affected by the activity life cycle then you should put those in a service.
     
  7. anaylor01

    anaylor01 Well-Known Member Licensed 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?
     
  8. Taha

    Taha Member Licensed 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.

     
  9. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to help without the relevant code.
     
  10. FHEBERT

    FHEBERT Member Licensed 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
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    When the phone is sleeping your process is paused. You can acquire a partial lock to prevent the CPU from going to sleep: Basic4android - Phone
    This will consume a lot of battery. Another better option is to use StartServiceAt and run the service each x minutes.
     
  12. BarrySumpter

    BarrySumpter Active Member Licensed 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
    Code:
    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(
    00'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: Mar 20, 2012
    merlin2049er likes this.
  13. BarrySumpter

    BarrySumpter Active Member Licensed 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.
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Nothing special should be done to convert it to a service. You should initialize the GPS in Service_Create. Move the code from Activity_Resume to Service_Start.
    And also move the other subs to the service.

    If it doesn't work for you then please post your service code.
     
    merlin2049er likes this.
  15. BarrySumpter

    BarrySumpter Active Member Licensed 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: Mar 26, 2012
  16. Erel

    Erel Administrator Staff Member Licensed User

  17. rfresh

    rfresh Well-Known Member Licensed 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

    Code:
    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...
     
  18. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Just schedule the service after the download completes.

    Sent from my GT-I9000 using Tapatalk 2
     
  19. rfresh

    rfresh Well-Known Member Licensed 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?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    You do not need to stop the service. Just call StartServiceAt each time to schedule the service to run in 10 minutes.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice