How to keep kivy service running in background in Android (service still run when switch to other App or lock the screen)?

后端 未结 2 1074
一个人的身影
一个人的身影 2021-01-24 04:38

I\'m trying to build an android APP with kivy,my requirement is quit simple:

When open the android APP,it will start counting from 0, and a prompt will pop up in the stat

相关标签:
2条回答
  • 2021-01-24 05:00

    is this code

    from jnius import autoclass
    service = autoclass(SERVICE_NAME)
    mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
    argument = ''
    service.start(mActivity, argument)
    

    inside the service.py or main.py

    0 讨论(0)
  • 2021-01-24 05:09

    After days test,I solved the problem

    My previous understanding of background operation is indeed problematic, or my solution is just one of the ways to achieve goal.

    in main.py,define the service name,e.g.

    SERVICE_NAME = u'{packagename}.Service{servicename}'.format(
        packagename=u'org.kivy.test',
        servicename=u'Myservice'
    )
    

    and in the place need to start service,

    from jnius import autoclass
    service = autoclass(SERVICE_NAME)
    mActivity = autoclass(u'org.kivy.android.PythonActivity').mActivity
    argument = ''
    service.start(mActivity, argument)
    

    in buildozer.spec,

    # (str) Package name
    package.name = test
    
    # (str) Package domain (needed for android/ios packaging)
    package.domain = org.kivy
    
    # (list) List of service to declare
    services = Myservice:service.py
    

    and then,edit the service.py as needed,the main.py and service.py can be communicated by oscpy

    By doing above,The notification can be popped up after open APP,even the APP is switched to background

    0 讨论(0)
提交回复
热议问题