Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get the service alive? #8

Open
mperezfra opened this issue Oct 22, 2020 · 1 comment
Open

Is there a way to get the service alive? #8

mperezfra opened this issue Oct 22, 2020 · 1 comment

Comments

@mperezfra
Copy link

When the phone goes to sleep or I change to other app for a while, Android kills the service. I get the following error

ActivityManager: Stopping service due to app idle u0a262 -1m55s386ms org.kivy.oscservice/.ServicePong

Is there a way to get the service alive in these situations?

@RobertFlatt
Copy link

@mperezfra
I changed the service to resume on app on_resume()
Because the stop() code does not work, I previously made these changes #9 (comment)

In on_resume(), I detect no response from the service, and restart it. Restart requires an elegant termination.

I expect there are issues that I have not considered, but this appears to work on Windows and Android.

Code fragments:

main .py:
server.bind(b'/alive', self.alive_response)

# ask if the service is alive
def ask_service_alive(self):
    self.im_alive = False
    self.client.send_message(b'/alive', [])
    # check for response after a short time
    Clock.schedule_once(self.check_alive,0.1)

# the response, which we may not get
def alive_response(self, message):
    self.im_alive = message.decode('utf8') == 'alive'

# check for no response
def check_alive(self, dt):
    if not self.im_alive:
        # lets hope whatever killed the service cleaned up afterwards
        self.start_service()

# a user might hope this would resume a service
def on_resume(self):
    if self.service:
        self.ask_service_alive(True)

service.py :

def alive():
    CLIENT.send_message(
        b'/alive',
        ['alive'.encode('utf8'), ],
    )

stopped = False

def stop(*_):
    global stopped
    stopped = True
    
if __name__ == '__main__':
    SERVER = OSCThreadServer()
    SERVER.listen('localhost', port=3000, default=True)
    SERVER.bind(b'/ping', ping)
    SERVER.bind(b'/stop', stop)
    SERVER.bind(b'/alive', alive)
    SERVER.getaddress()
    while True:
        sleep(1)
        if stopped:
            break
        send_date()
    SERVER.terminate_server()
    sleep(0.1)
    SERVER.close()

The choice to restart the service on_resume() is totally arbitrary. One could perhaps have two services restarting each other......

The unanswered question is what causes Android to stop the service? ( other than because it can)
These are not exactly helpful:
https://developer.android.com/guide/components/services#Basics
https://developer.android.com/reference/android/app/Service

More interesting how can one write a service that is unlikely to be stopped?
I have not seen an Android stop yet, but I'm restarting; and because of the way the demo is written I wouldn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants