gpt4 book ai didi

android - Qt android屏幕锁定java问题

转载 作者:行者123 更新时间:2023-11-30 05:40:57 25 4
gpt4 key购买 nike

我有 Qt 应用程序,需要在应用程序运行时防止屏幕变暗。我在 main.cpp 的 main() 开头放置了以下代码:

#ifdef ANDROID
// disable screen lock on android devices
QAndroidJniObject activity = QtAndroid::androidActivity();
if (activity.isValid()) {
QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");

if (window.isValid()) {
const int FLAG_KEEP_SCREEN_ON = 128;
window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
}
}
#endif

其中大部分来自:How to keep the screen on in Qt for android? , 除了我使用 callMethod<void>而不是 callObjectMethod .

问题是,当应用程序启动时,它会因大量 Java 日志而崩溃,我怀疑这是其中的相关部分:

F/art     (26455): art/runtime/runtime.cc:289] Pending exception android.view.ViewRootImpl$CalledFromWrongThreadException thrown by 'unknown throw location'
F/art (26455): art/runtime/runtime.cc:289] android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
F/art (26455): art/runtime/runtime.cc:289] at void android.view.ViewRootImpl.checkThread() (ViewRootImpl.java:6357)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.ViewRootImpl.requestLayout() (ViewRootImpl.java:874)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.View.requestLayout() (View.java:17476)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams) (View.java:11477)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.WindowManagerGlobal.updateViewLayout(android.view.View, android.view.ViewGroup$LayoutParams) (WindowManagerGlobal.java:305)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.WindowManagerImpl.updateViewLayout(android.view.View, android.view.ViewGroup$LayoutParams) (WindowManagerImpl.java:91)
F/art (26455): art/runtime/runtime.cc:289] at void android.app.Activity.onWindowAttributesChanged(android.view.WindowManager$LayoutParams) (Activity.java:2596)
F/art (26455): art/runtime/runtime.cc:289] at void org.qtproject.qt5.android.bindings.QtActivity.onWindowAttributesChanged(android.view.WindowManager$LayoutParams) (QtActivity.java:1385)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.Window.dispatchWindowAttributesChanged(android.view.WindowManager$LayoutParams) (Window.java:836)
F/art (26455): art/runtime/runtime.cc:289] at void com.android.internal.policy.impl.PhoneWindow.dispatchWindowAttributesChanged(android.view.WindowManager$LayoutParams) (PhoneWindow.java:3993)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.Window.setFlags(int, int) (Window.java:813)
F/art (26455): art/runtime/runtime.cc:289] at void android.view.Window.addFlags(int) (Window.java:771)

我向 AndroidManifest.xml 添加了适当的权限 (android.permission.WAKE_LOCK)。问题似乎是由 addFlags() 在其主视图之外运行引起的?我用谷歌搜索了很多,但找不到其他人遇到同样的问题。

谢谢,伊万

最佳答案

#if defined(Q_OS_ANDROID)
QAndroidJniObject activity = QtAndroid::androidActivity();
if (activity.isValid()) {
QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");

if (window.isValid()) {
const int FLAG_KEEP_SCREEN_ON = 128;
window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
}
QAndroidJniEnvironment env; if (env->ExceptionCheck()) { env->ExceptionClear(); } //Clear any possible pending exceptions.
}
#endif

here得到, 在 5.1 android 上很适合我

关于android - Qt android屏幕锁定java问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com