0

在过去的三个小时里,我一直在努力寻找解决方案。其他答案提到了这样做的原因是因为代码是在一个单独的线程中执行的,这是有道理的,但是作为一个不了解 Java 的人,再加上很少的文档pyjnius,我不知道如何翻译概述的类这里到`pyjnius':Android“只有创建视图层次结构的原始线程才能触摸它的视图。”

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from jnius import autoclass

CurrentActivity  = autoclass('org.kivy.android.PythonActivity').mActivity
view = CurrentActivity.getWindow().getDecorView()
view.setKeepScreenOn(True)

class Root(RelativeLayout):
    def __init__(self):
        super().__init__()

class SomeApp(App):
    def build(self):
        return Root()

SomeApp().run()

使用 android.view.Window 的不同方法时也会出现同样的错误:

CurrentActivity  = autoclass('org.kivy.android.PythonActivity').mActivity
WindowManager = autoclass('android.view.WindowManager$LayoutParams')
CurrentActivity.getWindow().addFlags(WindowManager.FLAG_KEEP_SCREEN_ON)

输出adb logcat python

12-15 16:48:21.692 18623 18646 I python  :  Traceback (most recent call last):
12-15 16:48:21.692 18623 18646 I python  : WARNING:kivy:stderr: Traceback (most recent call last):
12-15 16:48:21.693 18623 18646 I python  :    File "main.py", line 20, in <module>
12-15 16:48:21.693 18623 18646 I python  : WARNING:kivy:stderr:   File "main.py", line 20, in <module>
12-15 16:48:21.694 18623 18646 I python  :      view.setKeepScreenOn(True)
12-15 16:48:21.694 18623 18646 I python  : WARNING:kivy:stderr:     view.setKeepScreenOn(True)
12-15 16:48:21.695 18623 18646 I python  :    File "jnius/jnius_export_class.pxi", line 734, in jnius.JavaMethod.__call__ (jnius/jnius.c:25894)
12-15 16:48:21.695 18623 18646 I python  : WARNING:kivy:stderr:   File "jnius/jnius_export_class.pxi", line 734, in jnius.JavaMethod.__call__ (jnius/jnius.c:25894)
12-15 16:48:21.697 18623 18646 I python  :    File "jnius/jnius_export_class.pxi", line 828, in jnius.JavaMethod.call_method (jnius/jnius.c:27082)
12-15 16:48:21.697 18623 18646 I python  : WARNING:kivy:stderr:   File "jnius/jnius_export_class.pxi", line 828, in jnius.JavaMethod.call_method (jnius/jnius.c:27082)
12-15 16:48:21.698 18623 18646 I python  :    File "jnius/jnius_utils.pxi", line 93, in jnius.check_exception (jnius/jnius.c:4012)
12-15 16:48:21.698 18623 18646 I python  : WARNING:kivy:stderr:   File "jnius/jnius_utils.pxi", line 93, in jnius.check_exception (jnius/jnius.c:4012)
12-15 16:48:21.699 18623 18646 I python  :  jnius.JavaException: JVM exception occurred: Only the original thread that created a view hierarchy can touch its views.
12-15 16:48:21.699 18623 18646 I python  : WARNING:kivy:stderr: jnius.JavaException: JVM exception occurred: Only the original thread that created a view hierarchy can touch its views.
12-15 16:48:21.822 18623 18646 I python  : Python for android ended.
12-15 16:48:21.830 18623 18623 V SDL     : onWindowFocusChanged(): false
12-15 16:48:21.833 18623 18623 V PythonActivity: onPause()
12-15 16:48:21.833 18623 18623 V SDL     : onPause()
12-15 16:48:21.833 18623 18623 V SDL     : nativePause()
12-15 16:48:21.833 18623 18623 F libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 18623 (ample.myaasdqpp)
4

1 回答 1

0

您需要以下内容(未经测试):

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from jnius import autoclass
from android.runnable import run_on_ui_thread


class Root(RelativeLayout):
    def __init__(self):
        super().__init__()

class SomeApp(App):
    def build(self):
        self.do_window_stuff()
        return Root()

    @run_on_ui_thread
    def do_window_stuff(self):
        CurrentActivity  = autoclass('org.kivy.android.PythonActivity').mActivity
        view = CurrentActivity.getWindow().getDecorView()
        view.setKeepScreenOn(True)

SomeApp().run()

如果您只想为您的应用程序永久设置此项,您还可以通过 buildozer.spec 或--wakelockp4a 参数启用唤醒锁。

于 2018-12-15T20:10:27.317 回答