在 Android 中使用唤醒锁定无法打开屏幕 [英] Screen not turning on with wake lock in Android

查看:107
本文介绍了在 Android 中使用唤醒锁定无法打开屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个前台服务正在等待抖动.当它接收到这个动作时,它会发出一个广播:

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

还有我的开屏方法:

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

我有一个部分唤醒锁

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

在服务中声明

然而,当我关闭屏幕并晃动时,屏幕没有打开!我验证了每种方法都适用于日志.即使屏幕关闭,onReceive 也能正常工作.广播正在工作.就是屏幕打不开!

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!

推荐答案

调用WakeLocker类的acquire方法

call the acquire method of WakeLocker 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;
    }
}

需要权限:

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

这篇关于在 Android 中使用唤醒锁定无法打开屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆