[Android NDK] Wake lock

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

[Android NDK] Wake lock

Post 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;
    }
 
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: [Android NDK] Wake lock

Post 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
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: [Android NDK] Wake lock

Post 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.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
digijohnny
Posts: 63
Joined: Sat Sep 12, 2009 6:08 pm
Contact:

Re: [Android NDK] Wake lock

Post 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);
.....
..
.
Post Reply