open/resume app from service

523 views
Skip to first unread message

XR

unread,
Feb 20, 2015, 8:29:37 AM2/20/15
to kivy-...@googlegroups.com
Hi,

I try to open or resume an app from a service. The following code works for me when I start it from the main.py:

from jnius import autoclass, cast

PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Context = autoclass('android.content.Context')
activity
= cast('android.app.Activity', PythonActivity.mActivity)

# following line doesn't work
manager
= autoclass('android.content.pm.PackageManager')
# or:
# manager = activity.getPackageManager()

intent
= manager.getLaunchIntentForPackage("org.test.myapp")
intent
.addCategory(Intent.CATEGORY_LAUNCHER)

activity
.startActivity(intent)


But starting this code from a service either crashes kivy or simply doesn't work.
I tried the kivy launcher which mostly crashes. A builded app does nothing.


This is the debug output from the kivy launcher:
[WARNING           ] stderr: Traceback (most recent call last):
[WARNING           ] stderr:   File "main.py", line 60, in <module>
[WARNING           ] stderr:     intent = manager.getLaunchIntentForPackage("org.test.myapp")
[WARNING           ] stderr:   File "jnius_export_class.pxi", line 548, in jnius.jnius.JavaMethod.__call__ (jnius/jnius.c:17787)
[WARNING           ] stderr:   File "jnius_export_class.pxi", line 503, in jnius.jnius.JavaMethod.ensure_method (jnius/jnius.c:17236)
[WARNING           ] stderr: jnius.jnius.JavaException: Unable to find a None method!


Printing dir(manager) shows the method getLaunchIntentForPackage.

I had a look at jnius_export_class.pxi, but couldn't get any further with that.
https://github.com/kivy/pyjnius/blob/master/jnius/jnius_export_class.pxi

Does someone know a solution?

Regards,
XR



XR

unread,
Feb 21, 2015, 4:30:40 AM2/21/15
to kivy-...@googlegroups.com
Here is a complete example for the kivy launcher on Android.
Hopefully my problem gets more clear.

It works like it is described here:
http://kivy.org/planet/2014/01/building-a-background-application-on-android-with-kivy/

# -*- coding: utf-8 -*-

__version__
= '1.0'


import kivy
kivy
.require('1.0.7')

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from android import AndroidService

from jnius import autoclass, cast


class TestApp(App):

   
def build(self):
       
       
self.service = None
       
self.app_started = False
       
        root
= Widget()
       
        btn
= Button(text='start app', size_hint=(.5, .1),pos_hint={'center_x': .5, 'center_y': .5})
        btn
.bind(on_press=self.start_app)
       
        root
.add_widget(btn)
         
       
return btn    
   
   
   
def on_pause(self):  
       
       
if not self.app_started:
            service
= AndroidService('Service', 'service is running')
            service
.start('Hello From Service')
           
self.service = service
       
       
return True
       
       
   
def on_resume(self):

       
if self.service:
           
self.service.stop()
           
self.service = None
       
       
self.app_started = False
       
       
return True
   
   
   
def start_app(self,instance):
       
       
self.app_started = True

       
       
PythonActivity = autoclass('org.renpy.android.PythonActivity')
       
Intent = autoclass('android.content.Intent')
       
Context = autoclass('android.content.Context')
        activity
= cast('android.app.Activity', PythonActivity.mActivity)

       
       
#manager = autoclass('android.content.pm.PackageManager')
       
# or:
        manager
= activity.getPackageManager()
       
        intent
= manager.getLaunchIntentForPackage("org.kivy.touchtracer")
        intent
.addCategory(Intent.CATEGORY_LAUNCHER)
       
        activity
.startActivity(intent)
   
   
 
if __name__ == "__main__":

   
TestApp().run()






# -*- coding: utf-8 -*-


from jnius import autoclass, cast
from kivy.core.audio import SoundLoader
import time
 
 
 
def play_sound():
   
    sound
= SoundLoader.load("Bubamara.MP3")
    sound
.play()
    time
.sleep(1)
    sound
.stop()
   
 
if __name__ == '__main__':
   
   
# to see, if the service really gets started
   
# play a soundfile.
   
# place it in the service directory
   
   
#play_sound()


   
PythonActivity = autoclass('org.renpy.android.PythonActivity')
   
Intent = autoclass('android.content.Intent')
   
Context = autoclass('android.content.Context')
    activity
= cast('android.app.Activity', PythonActivity.mActivity)

   
   
# this line causes the error in kivys logfile:
   
# jnius.jnius.JavaException: Unable to find a None method!

    manager
= autoclass('android.content.pm.PackageManager')

   
   
# this line crashes kivy
   
# no error gets thrown
   
#manager = activity.getPackageManager()
     
    intent
= manager.getLaunchIntentForPackage("org.kivy.touchtracer")
    intent
.addCategory(Intent.CATEGORY_LAUNCHER)
     
    activity
.startActivity(intent)
   



Does someone know, if my problem is a generall restriction of what is possible with services? Or is it a bug or missing feature in kivy or pyjnius?
Or do I have to code it different?

Thanks for any help,
XR



XR

unread,
Feb 21, 2015, 7:22:23 AM2/21/15
to kivy-...@googlegroups.com
I think I found an error:
PythonActivity.mActivity is None in the service threat.

I'm new to kivy and to android also, so it's a lot of guessing at the moment for me.
Does anyone have a suggestion how to create an activity in the right way?

Alexander Taylor

unread,
Feb 21, 2015, 8:51:19 AM2/21/15
to kivy-...@googlegroups.com
You need mService from PythonService, not mActivity.

You can see how plyer does it at https://github.com/kivy/plyer/blob/master/plyer/platforms/android/__init__.py .

XR

unread,
Feb 21, 2015, 9:47:35 AM2/21/15
to kivy-...@googlegroups.com
Thanks! That solves it.

Regards,
XR

Donald Schwartfeger

unread,
Apr 24, 2015, 5:51:18 AM4/24/15
to kivy-...@googlegroups.com

Could you post your working solution to this as I still can't seem to get it to work trying to make changes as Alexander describes.

I am wanting to use this to wake a timer when time is up.

thank Donald

Donald Schwartfeger

unread,
Apr 27, 2015, 6:25:42 AM4/27/15
to kivy-...@googlegroups.com
I now have an app so it will resume from a service after a set time if I use the 'Home' button but not if I use 'Back' or the 'On/Off' buttons?

Using the 'On/Off' button seems to pause the app but not run the service or at least not resume when the time is up. It does resume when turned on with the 'ON/Off' button again.
The back button seems to exit the app and not run the service. Switching to the app again starts it from the beginning rather than resuming from where it was?

Can anyone enlighten me as to what state android goes into for the various buttons and how to resume my app from the different states?

BTW this is on a Samsung Galaxy S2 with Android 4.1.2 and using buildozer


thanks
Reply all
Reply to author
Forward
0 new messages