Android Tutorial Service Modules

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

  1. ericvanderhoeven

    ericvanderhoeven Member Licensed User

    Hi Umberto,

    the answer = yes

    From the service module, at your convenience (at that notification of new data or at a timer) you can just start any activity you'd like by using the command StartActivity(ActivityName)

    Easy does it..

  2. udg

    udg Expert Licensed User

    @Erel: sorry, I stand corrected. Just saw my question as a way to specify how to combine use of a service and an app.
    @eric: thanks, I'll procede that way.

  3. shmulik taiber

    shmulik taiber Member Licensed User

    hello Erel .
    I've managed to utilize a service module to get data from arduino via usb (asyncstreams) in the background . I've logged it (in the service module) and it's correct in realtime . however, accessing the data from Main module still evades me .
    I wrote -
    in the service module :
    str = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

    in Main
    or tried :

    got nothing . what am I doing wrong ?
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. luke2012

    luke2012 Well-Known Member Licensed User

    Hi @Erel.
    I'm using the StartServiceAt in order to schedule a notification in an specifica range (for example : 1 time in a month).
    The problem happen when the user restart the smartphone because the service is set to start at boot and the notification appears before the scheduled time.

    My target is to activate the notification only on the defined schedule time not at the system boot.
    Any solution about it?
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
  7. Roger C

    Roger C Active Member Licensed User

    A question about starting the service at a specific time...

    The task is to make a daily break in a timereport, when entering a new day, the time should be save for the old and a new event should be started for the next day.

    This could be done when the user checks out; check if it started on a previous day and then register yesterday as an event and then create and save a new event for the day he checked out.

    But I think (for closing each day correct, there might be need for running a report early in the day and the user might not have ended his shift yet) that I would try to have a service running at midnight and close the day before and start a new event.

    What is the best way to set the service to start at midnight every day?

    I have this code in Service_Start but are not sure that it will set 24 hours until next start. The date will probably be the same day...
    Should I add 1 to the date or should I set "00:00:00"?
    Can I always be sure that this Service will be called at exactly 23:59:59 every day without delays?

    Are there better ways to run a service at an exact time?

    Dim lTime As Long
    Dim start, endTime As Long

       start =
      endTime = 
    DateTime.DateTimeParse(, "23:59:59")
      lTime = 
    Abs(endTime - start)

    StartServiceAt("Midnight",lTime, True)
  8. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
  9. jlperez

    jlperez Member Licensed User

    Dear friends,
    I need to write a hidden service (no user interface) which needs to execute some task every 5 minutes.

    Could anyone give some advice on how can I build something like this without interface? How can I invoke such a service and install it on the smartphone?

    Thanks in advance

    Best regards
    Jose Luis
  10. Erel

    Erel Administrator Staff Member Licensed User

    Set StartAtBoot to true and use StartServiceAt to schedule the service to run every 5 minutes (search the forum there are many examples).

    Your app must run at least one time with an activity for the StartAtBoot to work. You can use a transparent activity.
  11. jlperez

    jlperez Member Licensed User

    Thank you Erel
  12. jefflynn1974

    jefflynn1974 Member Licensed User

    I have two services with start foreground. One of them is a widget. Can they use the same notification or every service must have an own one?
  13. Erel

    Erel Administrator Staff Member Licensed User

    You don't really need two foreground services. A single foreground service will prevent Android from killing the process. Other services will not be killed.
  14. jefflynn1974

    jefflynn1974 Member Licensed User

    Okay, thanks.
  15. Sahaya Arul

    Sahaya Arul Member Licensed User

    How to get a value from activity module to service module?
  16. lemonisdead

    lemonisdead Well-Known Member Licensed User

    By referencing using the activity's name. For example: n= Main.Variable
    The activity's variable should be declared in Process_Globals
  17. Sahaya Arul

    Sahaya Arul Member Licensed User

  18. entolium

    entolium Member Licensed User


    I have a service reading usbserial GPS. This service send the mocklocaction to the system, and call a activity sub (if not is paused) for show the data.

    The problem is when Android go off, or manually off pressing the power button (a short press). When power on (short press again), the service not update the data (the notify icon remains, then, I think the service is running ¿or not?). I must stop and start the service from the activity for return to normal operation.


    I'm crazy with this problem.
  19. Erel

    Erel Administrator Staff Member Licensed User

    Are you calling Service.StartForeground?
  20. entolium

    entolium Member Licensed User

    Thank you Erel for your respone. Yes, I call StartForeground.

    This is my code in Service Start:

    Sub Service_Start (StartingIntent As Intent)

    'StartServiceAt("", DateTime.Now + 5 * DateTime.TicksPerSecond, False) 'schedule the next task to run in 5 seconds.
        Service.StartForeground(1, Notificacion1)

    If usb.UsbPresent <> usb.USB_DEVICE Then
    "GPSusb","Dispositivo no compatible",Main)
    If (usb.HasPermission) Then
    Dim dev As Int
                    dev = usb.Open(
    If dev = usb.USB_DEVICE Then
    "GPSusb","Dispositivo conectado",Main)
    StartServiceAt(""DateTime.Now + 5 * DateTime.TicksPerSecond, True'schedule the next task to run in 5 seconds.
                        Notificacion1.SetInfo("GPSusb","Dispositivo no compatible",Main)
    End If
    End If
    StartServiceAt(""DateTime.Now + 5 * DateTime.TicksPerSecond, True'schedule the next task to run in 5 seconds.
    End Try
    End If

    End Sub
  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