Java API (pyjnius)¶
Using PyJNIus to access the Android API restricts the usage to a simple call of the autoclass constructor function and a second call to instantiate this class.
You can access through this method the entire Java Android API, e.g.,
the DisplayMetrics
of an Android device could be fetched using the
following piece of code:
DisplayMetrics = autoclass('android.util.DisplayMetrics')
metrics = DisplayMetrics()
metrics.setToDefaults()
self.densityDpi = metrics.densityDpi
You can access all fields and methods as described in the Java Android DisplayMetrics API as shown here with the method setToDefaults() and the field densityDpi. Before you use a view field, you should always call setToDefaults to initiate to the default values of the device.
Currently only JavaMethod, JavaStaticMethod, JavaField, JavaStaticField and JavaMultipleMethod are built into PyJNIus, therefore such constructs like registerListener or something like this must still be coded in Java. For this the Android module described below is available to access some of the hardware on Android devices.
Activity¶
If you want the instance of the current Activity, use:
PythonActivity.mActivity
if you are running an applicationPythonService.mService
if you are running a service
-
class
org.renpy.android.
PythonActivity
¶ -
mInfo
¶ Instance of an ApplicationInfo
-
mActivity
¶ Instance of
PythonActivity
.
-
registerNewIntentListener
(NewIntentListener listener)¶ Register a new instance of
NewIntentListener
to be called when onNewIntent is called.
-
unregisterNewIntentListener
(NewIntentListener listener)¶ Unregister a previously registered listener from
registerNewIntentListener()
-
registerActivityResultListener
(ActivityResultListener listener)¶ Register a new instance of
ActivityResultListener
to be called when onActivityResult is called.
-
unregisterActivityResultListener
(ActivityResultListener listener)¶ Unregister a previously registered listener from
PythonActivity.registerActivityResultListener()
-
-
class
org.renpy.android.
PythonActivity_ActivityResultListener
¶ 注釈
This class is a subclass of PythonActivity, so the notation will be
PythonActivity$ActivityResultListener
Listener interface for onActivityResult. You need to implementing it, create an instance and use it with
PythonActivity.registerActivityResultListener()
.-
onActivityResult
(int requestCode, int resultCode, Intent data)¶ Method to implement
-
-
class
org.renpy.android.
PythonActivity_NewIntentListener
¶ 注釈
This class is a subclass of PythonActivity, so the notation will be
PythonActivity$NewIntentListener
Listener interface for onNewIntent. You need to implementing it, create an instance and use it with
registerNewIntentListener()
.-
onNewIntent
(Intent intent)¶ Method to implement
-
Action¶
-
class
org.renpy.android.
Action
¶ This module is built to deliver data to someone else.
-
send
(mimetype, filename, subject, text, chooser_title)¶ Deliver data to someone else. This method is a wrapper around ACTION_SEND
Parameters: - mimetype: str
Must be a valid mimetype, that represent the content to sent.
- filename: str, default to None
(optional) Name of the file to attach. Must be a absolute path.
- subject: str, default to None
(optional) Default subject
- text: str, default to None
(optional) Content to send.
- chooser_title: str, default to None
(optional) Title of the android chooser window, default to ‘Send email...’
Sending a simple hello world text:
android.action_send('text/plain', text='Hello world', subject='Test from python')
Sharing an image file:
# let's say you've make an image in /sdcard/image.png android.action_send('image/png', filename='/sdcard/image.png')
Sharing an image with a default text too:
android.action_send('image/png', filename='/sdcard/image.png', text='Hi,\n\tThis is my awesome image, what do you think about it ?')
-
Hardware¶
-
class
org.renpy.android.
Hardware
¶ This module is built for accessing hardware devices of an Android device. All the methods are static and public, you don’t need an instance.
-
vibrate
(s)¶ Causes the phone to vibrate for s seconds. This requires that your application have the VIBRATE permission.
-
getHardwareSensors
()¶ Returns a string of all hardware sensors of an Android device where each line lists the informations about one sensor in the following format:
Name=name,Vendor=vendor,Version=version,MaximumRange=maximumRange,MinDelay=minDelay,Power=power,Type=type
For more information about this informations look into the original Java API for the Sensors Class
-
accelerometerSensor
¶ This variable links to a generic3AxisSensor instance and their functions to access the accelerometer sensor
-
orientationSensor
¶ This variable links to a generic3AxisSensor instance and their functions to access the orientation sensor
-
magenticFieldSensor
¶
The following two instance methods of the generic3AxisSensor class should be used to enable/disable the sensor and to read the sensor
-
changeStatus
(boolean enable)¶ Changes the status of the sensor, the status of the sensor is enabled, if enable is true or disabled, if enable is false.
-
readSensor
()¶ Returns an (x, y, z) tuple of floats that gives the sensor reading, the units depend on the sensor as shown on the Java API page for SensorEvent. The sesnor must be enabled before this function is called. If the tuple contains three zero values, the accelerometer is not enabled, not available, defective, has not returned a reading, or the device is in free-fall.
-
get_dpi
()¶ Returns the screen density in dots per inch.
-
show_keyboard
()¶ Shows the soft keyboard.
-
hide_keyboard
()¶ Hides the soft keyboard.
-
wifi_scanner_enable
()¶ Enables wifi scanning.
注釈
ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions are required.
-
wifi_scan
()¶ Returns a String for each visible WiFi access point
(SSID, BSSID, SignalLevel)
-
Further Modules¶
Some further modules are currently available but not yet documented. Please have a look into the code and you are very welcome to contribute to this documentation.