Screen not turning on with wake lock in Android

纵然是瞬间 提交于 2021-02-08 05:05:08

问题


I have a foreground service that is waiting for a shake. When it receives that motion, it sends out a broadcast with:

Intent i = new Intent("com.company.app.shakeDetectedMessage");
sendBroadcast(i);

My main activity receives this with a broadcast receiver which implements the method:

@Override
    public void onReceive(Context context, Intent intent)
    {
        MainActivity.this.turnOnScreen();
        Toast.makeText(getApplicationContext(), "Screen ON", Toast.LENGTH_SHORT).show();
    }

And my turn on screen method:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

I have a partial wake lock with

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
    PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
    mWakeLock.acquire();

declared in the service

Yet, when I turn my screen off and shake, the screen doesn't turn on! I verified that every method is working with logs. The onReceive is working even when the screen is off. The broadcast is working. Just the screen won't turn on!


回答1:


call the acquire method of WakeLocker class

Class :

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
        PowerManager.ACQUIRE_CAUSES_WAKEUP |
        PowerManager.ON_AFTER_RELEASE, "WakeLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

Permission Required:

<uses-permission android:name="android.permission.WAKE_LOCK" />   



回答2:


This is working for me

    PowerManager powerManager = getService(this, POWER_SERVICE);
    mScreenLock = powerManager.newWakeLock(
            PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP, "Lock");
    mScreenLock.acquire();


来源:https://stackoverflow.com/questions/27284794/screen-not-turning-on-with-wake-lock-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!