Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Service not always being restarted after being stopped

Service not always being restarted after being stopped

Scheduled Pinned Locked Moved Android
tutorialquestionmobilecomiot
5 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NormR1
    wrote on last edited by
    #1

    I'm developing an app for my tablet that is to turn on the screen when I rotate it to the vertical (the power switch is dodgy). It has an optimization to turn off the sensor from 22:00 to 06:30. The app works most of the time. However sometimes it fails. For example this morning I picked it up at 07:10 and the screen came on as expected. I put it down for about a half an hour and when I picked it up again it did not come on. I plugged in the power and it came on. I stopped my service and restarted it then and it has been running fine: pick up and it comes on when the screen has gone off. Looking at the logs I see that the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method. I've used SharedPreferences to save the app's state and am able to successfully restart the app where it needs to be. My question: why didn't the app work this morning after it had worked on the first pickup of the morning? Any ideas on how to debug this or any work-arounds? It's a very elusive bug. The code works so many times and fails so few times that I'm not able to come up with a debugging scheme to find out a reason or pattern for the failure. Do you have any suggestions? Something I've observed by looking at the logs is that the system restarts my service (passing a null intent to the onStartCommand() method) very soon after I have plugged in the tablet to turn on the screen. How would a bug in my code cause that? Another observation: the system often restarts my service 5 seconds after the AlarmReceiver is supposed to receive an alarm: at 22:00:05 for an alarm set for 22:00:00. Also posted at: http://www.codeproject.com/Questions/826395/Service-not-always-restarted-on-ASUS-tablet?cmt=683080#cmt2\_826395

    G 1 Reply Last reply
    0
    • N NormR1

      I'm developing an app for my tablet that is to turn on the screen when I rotate it to the vertical (the power switch is dodgy). It has an optimization to turn off the sensor from 22:00 to 06:30. The app works most of the time. However sometimes it fails. For example this morning I picked it up at 07:10 and the screen came on as expected. I put it down for about a half an hour and when I picked it up again it did not come on. I plugged in the power and it came on. I stopped my service and restarted it then and it has been running fine: pick up and it comes on when the screen has gone off. Looking at the logs I see that the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method. I've used SharedPreferences to save the app's state and am able to successfully restart the app where it needs to be. My question: why didn't the app work this morning after it had worked on the first pickup of the morning? Any ideas on how to debug this or any work-arounds? It's a very elusive bug. The code works so many times and fails so few times that I'm not able to come up with a debugging scheme to find out a reason or pattern for the failure. Do you have any suggestions? Something I've observed by looking at the logs is that the system restarts my service (passing a null intent to the onStartCommand() method) very soon after I have plugged in the tablet to turn on the screen. How would a bug in my code cause that? Another observation: the system often restarts my service 5 seconds after the AlarmReceiver is supposed to receive an alarm: at 22:00:05 for an alarm set for 22:00:00. Also posted at: http://www.codeproject.com/Questions/826395/Service-not-always-restarted-on-ASUS-tablet?cmt=683080#cmt2\_826395

      G Offline
      G Offline
      gupta avinash
      wrote on last edited by
      #2

      Quote:

      the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method

      Instead of returning START_STICKY from onStartCommand try returning START_REDELIVER_INTENT. This will ensure that the system redelivers the initial Intent that was passed to the service when the service was started.

      N 1 Reply Last reply
      0
      • G gupta avinash

        Quote:

        the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method

        Instead of returning START_STICKY from onStartCommand try returning START_REDELIVER_INTENT. This will ensure that the system redelivers the initial Intent that was passed to the service when the service was started.

        N Offline
        N Offline
        NormR1
        wrote on last edited by
        #3

        Thanks for the response. The null intent is not the problem. The code saves the data from the original intent and uses the startId value to recognize that is has been restarted. The problem is that my service is NOT being restarted promptly or some times not at all.

        G 1 Reply Last reply
        0
        • N NormR1

          Thanks for the response. The null intent is not the problem. The code saves the data from the original intent and uses the startId value to recognize that is has been restarted. The problem is that my service is NOT being restarted promptly or some times not at all.

          G Offline
          G Offline
          gupta avinash
          wrote on last edited by
          #4

          As you have mentioned that it is being killed frequently, it seems that the device is running low on resources and in such a case the system will not restart the Service until adequate resources are available. Try running the service in Foreground and see if it helps. However, if your device is running Android 4.4.2 then it's a known issue - https://code.google.com/p/android/issues/detail?id=63793[^]

          N 1 Reply Last reply
          0
          • G gupta avinash

            As you have mentioned that it is being killed frequently, it seems that the device is running low on resources and in such a case the system will not restart the Service until adequate resources are available. Try running the service in Foreground and see if it helps. However, if your device is running Android 4.4.2 then it's a known issue - https://code.google.com/p/android/issues/detail?id=63793[^]

            N Offline
            N Offline
            NormR1
            wrote on last edited by
            #5

            Thanks again. The link has some good ideas. My tablet is running version 4.3 I do use startForeground() method and it shows a Notification. The frequency of being stopped was almost exactly every 30 minutes. The times of some of the restarts are 5 seconds after an Alarm is supposed to go off and send an Intent to my service.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups