Android中的唤醒锁定屏幕无法启动

时间:2021-12-28 21:01:36

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!

然而,当我关闭屏幕并摇动时,屏幕无法开启!我验证了每种方法都在使用日志。即使屏幕关闭,onReceive也能正常工作。广播正在发挥作用。只是屏幕无法打开!

2 个解决方案

#1


5  

call the acquire method of WakeLocker class

调用WakeLocker类的acquire方法

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


0  

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();

#1


5  

call the acquire method of WakeLocker class

调用WakeLocker类的acquire方法

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


0  

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();