Kivy recipe: accessing main code and data from service

Generally the problem of communication between android service and the app itself is still not solved, the easiest way is to push it in service args. So let's do it.

Send data to service

In your app serialize needed data (for example in json). In my case it's configuration (I have not succeeded to access it from service, so I just transmit it this way)

    def build_service_params(self):
        params = {
            "enable_notifications": self.config.get('preferences', 'enable_notifications'),
            "morning_notification_time": self.config.get('preferences', 'morning_notification_time'),
            "lunch_notification_time": self.config.get('preferences', 'lunch_notification_time'),
            "evening_notification_time": self.config.get('preferences', 'evening_notification_time'),
        return params

And start the service like this


In service deserialize it like this

params = json.loads(os.getenv('PYTHON_SERVICE_ARGUMENT'))  

Access main code base

Also probably you will need to access main code from service, but it won't be loaded and you can't import it normally. I think it wouldn't be that good it import all the code once more in service, so I've hacked it like this

    settings = imp.load_compiled("settings", "../settings.pyo")
    database = imp.load_compiled("database", "../database.pyo")

You can import it as binary code, and it works! But you should take care of dependencies by yourself. Like in my case, database module depends on settings, so I need to import it before database.

Theoretically database is one more way to communicate between service and main code, but I don't think it's the best idea :-P

Hope it helps, good luck!

comments powered by Disqus