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

时间:2014-12-04 01:34:55

标签: java android eclipse oop wakelock

我有一个正在等待摇晃的前台服务。当它收到该动作时,会发出一个广播:

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

我的主要活动是通过实现方法的广播接收器接收到这个:

@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也能正常工作。广播正在发挥作用。只是屏幕无法开启!

2 个答案:

答案 0 :(得分:5)

调用WakeLocker类的获取方法

上课:

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" />   

答案 1 :(得分:0)

这对我有用

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