Android Question How to KeepAlive without Phone library

antonomase

Active Member
Licensed User
Longtime User
Hi,

I use the phone library just to call PhoneWakeState.KeepAlive and ReleaseKeepAlive.

Is it possible to call directly an android api to do the same thing without loading all this library ?

Thanks
 

Informatix

Expert
Licensed User
Longtime User
From my own library (I don't remember but I think that most of this code comes from Erel):
B4X:
private static PowerManager.WakeLock wakeLock;

public static void SetWakeLock(BA ba, boolean BrightScreen)
{
        if ((wakeLock != null) && (wakeLock.isHeld()))
        {
            BA.Log("WakeLock already held.");
            return;
        }
        PowerManager pm = (PowerManager)ba.context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock((BrightScreen ? PowerManager.SCREEN_BRIGHT_WAKE_LOCK : PowerManager.SCREEN_DIM_WAKE_LOCK)
                | PowerManager.ACQUIRE_CAUSES_WAKEUP, "B4A");
        wakeLock.acquire();
    }

    public static void ReleaseWakeLock()
    {
        if ((wakeLock == null) || (!wakeLock.isHeld()))
        {
            BA.Log("No wakelock.");
            return;
        }
        wakeLock.release();
}
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Informatix. I'm not 100% sure that the code you posted is exactly as it is in the library (although I'm sure Erel can confirm). The reason I say this is because I ALWAYS get a "No wakelock" message in the logs whenever I call ReleaseWakeLock - even in the cases when I know for sure that I have an active WakeLock. I don't know if this is because of some weird thing with Android where it doesn't report the current wake lock state correctly, or if the library just isn't checking it. No matter - it seems to work properly regardless.

Thanks - Colin.
 
Upvote 0
Top