Page 1 of 1

[Android NDK] Wake lock

Posted: Thu Sep 04, 2014 12:48 pm
by ent1ty
Wake locking the device is kinda fundamental in any game where you don't tap the display all the time. The NDK way is, as usual, quite painful and I hope my last incursion into JNI (seriously... next time, I'll just have some java code i'll be calling from the c++ side). Anyway, here it is in all it's glory

Code: Select all

 
    // constructing the lock
 
    JNIEnv* jni = 0;
    Context->App->activity->vm->AttachCurrentThread(&jni, NULL);
    if (jni)
    {
        jclass classNativeActivity = jni->FindClass("android/app/NativeActivity");
        jclass classPowerManager = jni->FindClass("android/os/PowerManager");
        if (classPowerManager)
        {
            jmethodID idNativeActivity_getAppContext = jni->GetMethodID(classNativeActivity, "getApplicationContext", "()Landroid/content/Context;");
 
            jobject AppContext = jni->CallObjectMethod(Context->App->activity->clazz, idNativeActivity_getAppContext);
            if (AppContext)
            {
                jmethodID idNativeActivity_getSystemService = jni->GetMethodID(classNativeActivity, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
                jstring POWER_SERVICE = jni->NewStringUTF("power");
                jobject PowerManager = jni->CallObjectMethod(AppContext, idNativeActivity_getSystemService, POWER_SERVICE);
                if (PowerManager)
                {
                    jmethodID idPowerManager_newWakeLock = jni->GetMethodID(classPowerManager, "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;");
                    int SCREEN_BRIGHT_WAKE_LOCK = 0x0000000a;
                    long int ON_AFTER_RELEASE = 0x20000000;
                    jstring jWakeLockTag = jni->NewStringUTF("com.entity.Polygame.MY_BRIGHT_SCREEN_LOCK_LOL");
 
                    WakeLock = jni->CallObjectMethod(PowerManager, idPowerManager_newWakeLock, SCREEN_BRIGHT_WAKE_LOCK | ON_AFTER_RELEASE, jWakeLockTag);
                    debugLog("constructed wake lock");
 
                    jni->DeleteLocalRef(jWakeLockTag);
                }
 
                jni->DeleteLocalRef(POWER_SERVICE);
            }
 
        }
 
        Context->App->activity->vm->DetachCurrentThread();
    }
 
 
    // acquire / release (the lock is in "released" state when first constructed)
 
    JNIEnv* jni = 0;
    Context->App->activity->vm->AttachCurrentThread(&jni, NULL);
    if (jni)
    {
        jclass wakeLock = jni->FindClass("android/os/PowerManager$WakeLock");
 
        if (locked) to acquire the lock
        {
            jmethodID idWakeLock_acquire = jni->GetMethodID(wakeLock, "acquire", "()V");
 
            jni->CallVoidMethod(WakeLock, idWakeLock_acquire);
 
            debugLog("acquired wake lock");
        }
        else // release the lock
        {
            jmethodID idWakeLock_release = jni->GetMethodID(wakeLock, "release", "()V");
 
            jni->CallVoidMethod(WakeLock, idWakeLock_release);
 
            debugLog("released wake lock");
        }
 
        Context->App->activity->vm->DetachCurrentThread();
        Active = locked;
    }
 

Re: [Android NDK] Wake lock

Posted: Thu Sep 04, 2014 1:54 pm
by Nadro
imho this is better solution for keeping the display awake:

Code: Select all

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Inside onCreate method in Java activity

Re: [Android NDK] Wake lock

Posted: Thu Sep 04, 2014 7:20 pm
by ent1ty
Yes I've looked at that solution as well, however I read that was causing the screen to dim after the timeout time rather than stay at full brightness? Not sure if that's true now that I'm thinking about it.

Re: [Android NDK] Wake lock

Posted: Mon Apr 29, 2019 6:58 pm
by digijohnny
Update:
if you need to do this in Pure NDK
in
void android_main(android_app* app){
........
ANativeActivity_setWindowFlags(app->activity,AWINDOW_FLAG_KEEP_SCREEN_ON , 0);
.....
..
.