有没有办法通过JNI / NDK在Android上获取唤醒锁?

时间:2013-07-15 12:21:04

标签: android android-ndk android-wake-lock

我正试着在我的Android手机上拿到一个唤醒锁,但我找不到任何关于通过NDK / JNI在c / cpp中执行此操作的信息。
我一直googlin这么久我看不出来。我所看到的只是Java Java Java,我需要一些C.我会做一点C。 :)

有谁知道我在哪里找到一小部分能在Android设备上获得唤醒锁的C?

为什么我需要它...我正在创建一个lib来执行一项非常具体的任务,我希望将wakelock作为该lib的一部分。保持需要在一起的东西,而不是在我不需要的地方混合我的代码。

3 个答案:

答案 0 :(得分:1)

我认为一个简单的方法就是做这样的事情

system("echo 'mylock' > /sys/power/wake_lock");

答案 1 :(得分:0)

如果您愿意,您绝对可以用C语言完成所有工作。但要正确获取唤醒锁定,至少需要ApplicationContext。这意味着您的natvie代码仍将依赖于Java级别的协作,例如NativeActivity。

您只需使用JNI查找必要的类和方法ID。

答案 2 :(得分:-1)

这只能使用JNI来完成,因为dex引擎处理唤醒锁控制。发布了一些代码,忽略了所有错误检查和异常处理:http://irrlicht.sourceforge.net/forum/viewtopic.php?f=9&t=50228 (如果链接断开,则无耻地复制和粘贴谷歌读者。)

// 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;
}