Qt android屏幕锁定java问题



我有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

这几乎来自:如何保持屏幕在Qt的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)

我添加了适当的权限(android.permission.WAKE_LOCK)到AndroidManifest.xml。问题似乎是由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

从这里得到它,为我在5.1 android好

我最终在Java中做了这个。

下面是java代码:
package org.qtproject.visualization;
import org.qtproject.qt5.android.bindings.*;
import android.os.Bundle;
import android.view.WindowManager;
public class ScreenOnActivity extends QtActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
        super.onCreate(savedInstanceState);
    }
}

在Android上部署时,Qt实际上并不运行在主应用程序(Java)线程中。由于android.view.Window.addFlags必须从Java主线程调用,因此您很难回到该线程以使此代码工作。

最好是使用WakeLock来防止系统进入休眠模式。这里有一篇文章解释了如何做到这一点。

相关内容

  • 没有找到相关文章

最新更新