Welcome!

This is the seventh post about mobile development with Python. In this post, I’ll show you how to work with PyJNIus, a Python module to access Java classes as Python classes using JNI. To show how it works, we’ll create a simple share button to open an intent and share a link with an external application 🙂

I – About the tutorials

(If you had read another tutorial, you can go to the topic III, because I and II is the same in all tutorials)

All my next posts will be about this tutorial series. I’ll show you how to use each mobile API, like compass, camera, sensors, sound, and others. After the API, we will start to test some Python libs, as PyGame and OpenCV. I’m using Android with buildozer, but some libs are compatible with iOS too(I’ll tell you when it’s compatible), so if you test it, send a feedback if possible.

In each post, I’ll show a sample app using on of them. When I finish all the tutorial, you will be able to develop almost any app with Python. In the principle, I’m not going to show the design stuff, but I recommend you to check Kivy and PySide. After finishing theses tutorials, maybe I can start a tutorial series about Kivy, for the design, but it’s easier to find at Google 🙂

You can check the Github repo here, and if you like it, please, star it 🙂

To be able to read all the tutorials, please follow my blog or subscribe your email to get updates. Another important thing if your feedback. I’m Brazilian and trying to write in English, so if you have any problem, please, just let me know. To make the tutorials better ,try to comment always that is possible, showing your experiences, tips for a better tutorial, and this kind of stuff.

Before start, you can check this links to configure all the prerequisites:

II – Index

You will be able to get the links for all tutorials here, I’ll be updating when a new tutorial is published.

Tutorial #1 – Accelerometer

Tutorial #2 – Camera

Tutorial #3 – Kivy Designer

Tutorial #4 – Debug

Tutorial #5 – Android and iOS game with Python

Tutorial #6 – Reading and saving user data

Tutorial #7 – Use Android classes with PyJNIus

III – About this tutorial

This is a short post, just to show you how to start an Android Intent with Kivy. In this post I’ll be using Pyjnius, a Python module to access Java classes as Python classes using JNI. It’s explaining more about the game created at Tutorial #5 – Android and iOS game with Python

IV – Creating our project

Let’s start to work on it. Create a new folder, and create two files, main.py, for the source code, and main.kv, the UI. Now, start a new buildozer project with the command:

buildozer init

As we are using an external library, Pyjnius, you need to add it to your buildozer.spec file. Open your buildozer.spec and replace add pyjnius to the following line:

requirements = kivy, pyjnius

Now to make sure that you are able to run this project, run the following command:

buildozer –verbose android debug deploy run

This command will prepare your environment, check requirements and run the app on Android. If you get some error, please comment here so I can try to help you.

Now, let’s create a simple UI to test the Android Intent.

V – A simple UI with KV Lang

In this post we’ll have just a simple BoxLayout with a Button, nothing beautiful ;/ Just add the following code to your main.kv:

<RootWidget>: Button: text: 'Share simple text' on_press: root.share()

With this code, the parent widget will be a RootWidget, a Python class that we’ll create later. The RootWidget has a simple Button, with a text and on_press event. When the user clicks on the button, it will can the method share() on RootWidget class.

VI – Python Code

Let’s add just the necessary to run our app. Use the following code in main.py:

__version__ = '1.0' from kivy.app import App from kivy.uix.boxlayout import BoxLayout class RootWidget(BoxLayout): def share(self): print(&quot;sharing&quot;) class MainApp(App): def build(self): return RootWidget() def on_pause(self): return True if __name__ == '__main__': MainApp().run()

It’s simple! We just added a RootWidget that expends a BoxLayout, and created the method share().

VII – Working with PyJNIus

Now, let’s learn more about PyJNIus and use it.

PyJNI can wrap Java classes to use it with Python! Take a look in this simple example provided by PyJNIus documentation:

System = autoclass('java.lang.System') System.out.println('Hello World')

With these commands you will be able to call Java System.out.println!

VIII – Starting an Android Intent with Kivy and PyJNIus

Now, let’s integrate PyJNIus with our Android Kivy application.

You can read the Android documentation here: http://developer.android.com/training/sharing/send.html

Add these lines to the top of your file:

from kivy.utils import platform if platform() == "android": from jnius import cast from jnius import autoclass

As long as we can share only with Android, first we need to check the running platform and then import the required libraries.

Now, just update the share():

def share(self): if platform() == 'android': #check if the app is on Android # read more here: http://developer.android.com/training/sharing/send.html PythonActivity = autoclass('org.renpy.android.PythonActivity') #request the Kivy activity instance Intent = autoclass('android.content.Intent') # get the Android Intend class String = autoclass('java.lang.String') # get the Java object intent = Intent() # create a new Android Intent intent.setAction(Intent.ACTION_SEND) #set the action # to send a message, it need to be a Java char array. So we use the cast to convert and Java String to a Java Char array intent.putExtra(Intent.EXTRA_SUBJECT, cast('java.lang.CharSequence', String('Byte::Debugger() Tutorial #7'))) intent.putExtra(Intent.EXTRA_TEXT, cast('java.lang.CharSequence', String(&quot;Testing Byte::Debugger() Tutorial #7, with Python for Android&quot;))) intent.setType('text/plain') #text message currentActivity = cast('android.app.Activity', PythonActivity.mActivity) currentActivity.startActivity(intent) # show the intent in the game activity

And here we can the the Android Intent working:

That’s it. I’ll be writing more tutorials soon.

Thanks for reading!

Aron Bordin

Hey!! I’m available for freelances!! If you need anything, just contact me!