Last active
June 18, 2022 04:20
Examples using PyJNIus to create Java class handlers in Python on Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from jnius import PythonJavaClass, java_method, autoclass, cast | |
from threading import current_thread | |
from kivy.uix.button import Button | |
from kivy.clock import Clock | |
from functools import partial | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.progressbar import ProgressBar | |
def mainthread(func): | |
def delayed_func(*args): | |
Clock.schedule_once(partial(func, *args), 0) | |
return delayed_func | |
class Generic3AxisSensor(PythonJavaClass): | |
__javainterfaces__ = ['android/hardware/SensorEventListener'] | |
def __init__(self, app): | |
super(Generic3AxisSensor, self).__init__() | |
self.app = app | |
print '---------------> AUTOCLASS' | |
PythonActivity = autoclass('org.renpy.android.PythonActivity') | |
Context = autoclass('android.content.Context') | |
Sensor = autoclass('android.hardware.Sensor') | |
SensorManager = autoclass('android.hardware.SensorManager') | |
print '---------------> CREATE MANAGER' | |
self.manager = cast('android.hardware.SensorManager', | |
PythonActivity.mActivity.getSystemService(Context.SENSOR_SERVICE)) | |
print '---------------> GET DEFAUTL SENSOR' | |
self.sensor = self.manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | |
print '---------------> REGISTER LISTENER' | |
self.manager.registerListener(self, self.sensor, | |
SensorManager.SENSOR_DELAY_NORMAL) | |
print '---------------> DONE' | |
print '------> CURRENT THREAD IS', current_thread() | |
@java_method('(Landroid/hardware/Sensor;I)V') | |
def onAccuracyChanged(self, sensor, accuracy): | |
print 'onAccuracyChanged()', sensor, accuracy | |
@java_method('(Landroid/hardware/SensorEvent;)V') | |
def onSensorChanged(self, event): | |
print 'onSensorChanged()', event | |
print 'onSensorChanged() event.values =', event.values | |
print '-> CURRENT THREAD IS', current_thread() | |
self.app.update_values(event.values) | |
class SensorApp(App): | |
def start_acc(self, *args): | |
if self.sensor is None: | |
self.sensor = Generic3AxisSensor(self) | |
@mainthread | |
def update_values(self, values, *args): | |
for index, x in enumerate(values): | |
self.root.children[index].value = 50 + x | |
def build(self): | |
#root = Label(text='Accelerometer', font_size='40sp') | |
self.sensor = None | |
root = BoxLayout(orientation='vertical') | |
root.add_widget(Button(text='Start', font_size='40sp', | |
on_release=self.start_acc)) | |
root.add_widget(ProgressBar(value=0)) | |
root.add_widget(ProgressBar(value=0)) | |
root.add_widget(ProgressBar(value=0)) | |
return root | |
def on_pause(self): | |
print '-----------------------> PAUSE????' | |
return True | |
if __name__ == '__main__': | |
SensorApp().run() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# need --permission ACCESS_FINE_LOCATION --permission ACCESS_COARSE_LOCATION | |
# few methods like onStatusChanged are not implemented in this example. | |
from kivy.app import App | |
from jnius import PythonJavaClass, java_method, autoclass | |
from kivy.uix.label import Label | |
from kivy.clock import Clock | |
from functools import partial | |
def mainthread(func): | |
def delayed_func(*args): | |
Clock.schedule_once(partial(func, *args), 0) | |
return delayed_func | |
Looper = autoclass('android.os.Looper') | |
LocationManager = autoclass('android.location.LocationManager') | |
PythonActivity = autoclass('org.renpy.android.PythonActivity') | |
Context = autoclass('android.content.Context') | |
class GpsListener(PythonJavaClass): | |
__javainterfaces__ = ['android/location/LocationListener'] | |
def __init__(self, app): | |
self.app = app | |
super(GpsListener, self).__init__() | |
@java_method('()I') | |
def hashCode(self): | |
return id(self) | |
@java_method('(Landroid/location/Location;)V') | |
def onLocationChanged(self, location): | |
print 'onLocationChanged() called', location | |
print 'lat', location.getLatitude() | |
print 'lon', location.getLongitude() | |
self.app.update_location(location) | |
class GpsApp(App): | |
@mainthread | |
def update_location(self, location, *args): | |
self.root.text = 'lat: {}\nlon: {}'.format( | |
location.getLatitude(), | |
location.getLongitude()) | |
def build(self): | |
locationManager = PythonActivity.mActivity.getSystemService( | |
Context.LOCATION_SERVICE) | |
listener = GpsListener(self) | |
locationManager.requestLocationUpdates( | |
LocationManager.GPS_PROVIDER, | |
10000, | |
10, | |
listener, | |
Looper.getMainLooper()) | |
root = Label(text='Should listen to the GPS!', font_size='40sp') | |
return root | |
if __name__ == '__main__': | |
GpsApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment