10

I am trying to build android app using kivy. How I can hide my app but still keep it running in the background like a deamon?

from kivy.config import Config
Config.set('graphics', 'fullscreen', 'fake')

from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
    def build(self):
        button = Button(text="Exit", size_hint=(None, None))
        button.bind(on_press=exit)
        return button

if __name__ == '__main__':
    MyApp().run()
user
  • 5,370
  • 8
  • 47
  • 75
sam
  • 18,509
  • 24
  • 83
  • 116

2 Answers2

8

You need to use an android service if you want to actually do computation in the background. Python-for-android can do this, the relevant documentation is here (old_toolchain). For the new toolchain method see here.

If you just want your app to not be closed completely (so that it doesn't restart entirely with the splash screen etc. every time), you just have to add an on_pause method to your App class, and it should return True. You can also do any pre-pause stuff in this method. However, the app doesn't really keep running, it just keeps memory state.

In the latter case, be aware that android can and sometimes will kill apps in a pause state. This is a normal part of the way apps are handled and you can't avoid it, so you should save any important state in your on_pause method.

Andre Miras
  • 3,580
  • 44
  • 47
inclement
  • 29,124
  • 4
  • 48
  • 60
  • just by using start_service, my application will run in background irrespective of its closed? – sam Dec 21 '13 at 17:36
  • 1
    No, the service is a separate process that only runs what you specifically add to it. – inclement Dec 21 '13 at 23:42
  • can you please give me any example of services. It will help me to understand more because I didnt understand how can I my app to services. how will i start a separate process for my application? – sam Dec 22 '13 at 03:48
  • 1
    The docs seem to have moved to [here](http://python-for-android.readthedocs.io/en/latest/old_toolchain/android/#service-android-service) (I'm only unsure about the "old_toolchain" part) – Andras Deak -- Слава Україні May 19 '16 at 01:13
  • 1
    Thanks @AndrasDeak I've edited the answer to link to old and new docs. – Andre Miras Sep 09 '18 at 15:59
0

As Inclement already mentioned, you need to start an android service for this. This kivy planet post (which appeared after you asked the question) gives a walk through of how to have a program, a service, and have them interact with each other.

dirkjot
  • 3,467
  • 1
  • 23
  • 17