如何在qt应用程序运行时防止Android设备进入睡眠状态

时间:2014-04-16 14:15:03

标签: qt qt-necessitas

我有一个在qt中开发的幻灯片的Android应用程序。一段时间后,设备会使屏幕变暗,然后关闭并锁定。在用户退出应用程序之前,我该如何避免这种情况。

类似于“android.permission.WAKE_LOCK”并调用一些函数..我不确定。

1 个答案:

答案 0 :(得分:0)

你需要执行这段java代码:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

为此,请将此文件添加到项目中:

<强> WakeLock.java:

package my.utl;

import org.qtproject.qt5.android.bindings.QtActivity;
import org.qtproject.qt5.android.bindings.QtApplication;
import java.lang.String;
import android.app.Activity;
import android.os.PowerManager;
import android.content.Context;

public class WakeLock
{
    private Activity myActivity;
    protected PowerManager.WakeLock m_WakeLock = null;

    public WakeLock(Activity a)
    {
        myActivity = a;
    }

    public int configure()
    {
        System.out.println("Inside WakeLock::configure");

        try
        {
            final PowerManager pm = (PowerManager) myActivity.getSystemService(Context.POWER_SERVICE);

            m_WakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
            // m_WakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
            // Does not work: https://stackoverflow.com/questions/5183859/partial-wake-lock-vs-screen-dim-wake-lock-in-download-thread
            m_WakeLock.acquire();

            return 42;
        }
        catch (Exception e)
        {
            System.out.println("WakeLock failed: " + e.toString());
        }

        return -1;
    }
}

然后,从您的应用中,只需执行代码:

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");    //activity is valid
if ( activity.isValid() )
{
    QAndroidJniObject wakeLockObject("my/utl/WakeLock","(Landroid/app/Activity;)V",activity.object<jobject>());
    if ( wakeLockObject.isValid() )
    {
        jint res = wakeLockObject.callMethod<jint>("configure","()I");
        assert( res == 42 ); // check Java code was executed...
    }
    else
    {
        assert( false );
    }
}
else
{
    assert( false );
}

没有java文件可能有办法做到这一点。我在做这个工作: Unable to call PowerManager.WakeLock.newWakeLock using QAndroidJniObject