Android service from kivy/pyjnius

1,009 views
Skip to first unread message

BSG

unread,
Apr 9, 2013, 2:52:55 AM4/9/13
to kivy-...@googlegroups.com
Hi,

I'm new to android and kivy development, so apologies for this newbie question. 

I am trying to get kivy through pyjnius to create an android service from a custom java class (doing it with pure kivy/pyjnius and python did not seem to work).  I would like to know how kivy/pyjnius finds custom java classes. 

Intent = autoclass('android.content.Intent')
PythonActivity = autoclass('org.renpy.android.PythonActivity')        
activity
= cast('android.app.Activity', PythonActivity.mActivity)
context
= activity.getApplicationContext()
#something like this in Java:
#Intent intent = new Intent(context, AndroidService.class);
intent
= Intent()
intent
.setClassName(context, 'org.myapp.AndroidService')   #this is where I'm stuck
#source file built from dist/default/src/org/myapp/AndroidService.java
context
.startService(intent)        

I put the service tag inside the AndroidManifest.xml file before running the build script, but the service is still "not found".  Is there something else I need to put in the manifest in order register the service?

<application>
  ...
 
<service android:name="AndroidService" />
</application>

If someone could point me in the right direction as how to create android services with kivy and pyjnius, I would be grateful.

Thanks
BSG

deakblue

unread,
Apr 9, 2013, 4:30:20 PM4/9/13
to kivy-...@googlegroups.com
Shot in the dark, I think you need an autoclass'ed context reference.  But, I think you can skip the Context part entirely and just use the activity, both ideas below:

See if:

context = autoclass('android.content.Context')
#and/or possibly this helps
context = cast('android.content.Context', activity.getApplicationContext())

Or skipping context entirely and just using just Activity (because it IS a context):
...
intent.setClassName(activity, 'org.myapp.AndroidService')   #this is where I'm stuck

#source file built from dist/default/src/org/myapp/AndroidService.java
activity.startService(intent) 

I am working with a lot of services on my android app and I take a slightly different approach, but YMMV.  In short, I treat the PythonActivity like it's a Facade to the rest of Android.  So service creation methods are exposed there, but I code the actual creation work in Java and keep the interface pretty simple.  

In order to make development and testing go by faster, I use a dummy activity that is Android-only to stand in while I sort out the java side.  Then I just port the hook methods across to PythonActivity when I'm done.  I know this makes pyjnius a less sexy, but it has some limitations (for example, it's a door in, but there's currently no "calling from" Java, not sure about Exceptions either).

Best regards,
-db-
Reply all
Reply to author
Forward
0 new messages