Android Tutorial Service Modules

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

  1. entolium

    entolium Member Licensed User

    When I press the power button, and the screen turn on, the Notify is running and showing the message "Device connected".
    Is possible the problem could be the astreams?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    It is however possible that the USB connection is broken when the device is sleeping.

    You can try to acquire a partial lock to keep the CPU running. Are you handling AStream error and terminated events?
     
  3. entolium

    entolium Member Licensed User

    Thank you Erel,

    This is the code:

    Code:
    Sub AStreams_Error
        astreams.Close
        
    StartServiceAt(""DateTime.Now + 2 * DateTime.TicksPerSecond, True'schedule the next task to run in 2 seconds.
    End Sub

    Sub Astreams_Terminated
        astreams.Close
        
    StartServiceAt(""DateTime.Now + 2 * DateTime.TicksPerSecond, True'schedule the next task to run in 2 seconds.
    End Sub
    The objetive is use a tablet like car GPS, with an usb antena. The gps never must fail, and must recover it in any error.
    I'm trying to restart the service in all errors and when power on, but nothing works.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I recommend you to add some log messages to better understand what happens.

    Try to acquire a partial lock and see whether the system continues running. You can also wake up the device with a standard lock.
     
  5. entolium

    entolium Member Licensed User

    Thank you Erel,

    I can't debug the app. The usb is used by the gps and througt wifi I have problems and the debugger stops.

    When power off, the GPS antenna power off (the led off). I add toast message in all lines, and I think the only one problem is the connection usb breaks (the astream don't fire any error).
    With a Timer I could monitor the usb state, but I need the usb connection state property for check, and the library don't have it :(.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this. There might be other ways to find that the USB has disconnected.
     
  7. entolium

    entolium Member Licensed User

    More questions, I'm sorry.

    I have one activity and one service.

    When execute "StartService(..." (Pressing F8), approximately 12 seconds after (the yellow bar is waiting in the line "Sub Service_Create"), whitout touching anything, the service run the code o_O

    What is happening?
     
  8. entolium

    entolium Member Licensed User

    I'm resolved. I added "Startservice" in Activity_Resume. My app is linked with the usb device, then, when the system return from deep, resume the app and the service start again.
     
  9. MrKim

    MrKim Active Member Licensed User

    IF I start a service and then open a MsgBox will the service continue to run?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    The service will continue to run. However some events may be queued until the msgbox is dismissed.
     
  11. LucaMs

    LucaMs Expert Licensed User

    This is related to the service modules? Can not a service use a module's routine simply by invoking its name?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    No.
    Yes, you can.
     
  13. incendio

    incendio Well-Known Member Licensed User

    I wonder about the use of Service.

    Suppose I have a service to download a lot of data from internet at the specific time, since service is run in the same thread with other activities, it will freeze user interaction when service is running, right?
     
  14. LucaMs

    LucaMs Expert Licensed User

    I would say wrong. One of the reasons to using the services is to delegate them long works to execute in background.

    As Erel says in the main post:
    - Running a long operation. For example downloading a large file from the internet. In this case you can call Service.StartForeground (from the service module). This will move your activity to the foreground state and will make sure that the OS doesn't kill it. Make sure to eventually call Service.StopForeground.

    The first example, moreover, does just that.
     
  15. incendio

    incendio Well-Known Member Licensed User

    Even running on the same thread, it won't interrupt current process?

    Isn't it, when running on the same thread all processes must be carried in order, mean you can't do next process if previous process not finished yet.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    No. All the libraries that handle network communication (including HttpUtils2) make sure to use background threads for the communication.
     
  17. incendio

    incendio Well-Known Member Licensed User

    OK, but after download finished, service will do something with this data, before presented to users, may be doing some calculations, saving to database, etc.

    This process could take times, and before the process finished, won't it delayed other process?
     
  18. LucaMs

    LucaMs Expert Licensed User

    Well, the CPU is one, then yes.
     
  19. incendio

    incendio Well-Known Member Licensed User

    It is not about CPU, it's about thread.

    Even there are more CPU, if running on the same thread, I think it could delayed other process.

    The solution that I can think right now is separate main application with service app, but I don't know if service app able to read or right main app data if this data located on directory internal.
     
    Last edited: Sep 19, 2014
  20. LucaMs

    LucaMs Expert Licensed User

    You wrote "delayed" not interrupt.

    Since I'm starting now to use the service modules, I'm not sure.

    The previous Erel's answer suggests that the communication takes place in a different thread, but it is not clear if ONLY for communication.

    But there is an optimal solution: try it!

    ;)
     
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