Skip to content

Instantly share code, notes, and snippets.

@kived
Created February 9, 2015 20:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kived/4b3c1a78b0104e52b2a1 to your computer and use it in GitHub Desktop.
Save kived/4b3c1a78b0104e52b2a1 to your computer and use it in GitHub Desktop.
Kivy: Android keep screen on
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.lang import Builder
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
View = autoclass('android.view.View')
Params = autoclass('android.view.WindowManager$LayoutParams')
from android.runnable import run_on_ui_thread
root = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
Button:
text: 'Set Flag'
on_press: app.setflag()
Button:
text: 'Clear Flag'
on_press: app.clearflag()
''')
class DimTestApp(App):
def build(self):
return root
#@run_on_ui_thread
#def set_systemui_visibility(self, options):
# PythonActivity.mActivity.getWindow().getDecorView().setSystemUiVisibility(options)
#
#def dim(self, *args):
# self.set_systemui_visibility(View.SYSTEM_UI_FLAG_LOW_PROFILE)
#
#def undim(self, *args):
# self.set_systemui_visibility(0)
@run_on_ui_thread
def android_setflag(self):
PythonActivity.mActivity.getWindow().addFlags(Params.FLAG_KEEP_SCREEN_ON)
def setflag(self, *args):
self.android_setflag()
@run_on_ui_thread
def android_clearflag(self):
PythonActivity.mActivity.getWindow().clearFlags(Params.FLAG_KEEP_SCREEN_ON)
def clearflag(self, *args):
self.android_clearflag()
if __name__ == '__main__':
DimTestApp().run()
@frankgould
Copy link

This isn't working for me. When I compile it in my app, it crashes my app at the startup with can't find main.py/main.pyo. I then have to reboot my mac to recompile a version without the run_on_ui_thread. I'm using kivy and buildozer, if there something I'm missing in the environment. I tried to compile this version but it crashed the same on startup. Please advise. Thanks!

@dfrison01
Copy link

Works as long as I change 'org.renpy.android.PythonActivity' ==> 'org.kivy.android.PythonActivity'

@faisal-adraji
Copy link

faisal-adraji commented May 23, 2018

to keep screen on, go to buildozer.spec file and change the following lines:

# (list) Permissions
android.permissions = WAKE_LOCK
...............................
# (bool) Indicate whether the screen should stay on
# Don't forget to add the WAKE_LOCK permission if you set this to True
android.wakelock = True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment