唤醒锁不起作用

时间:2015-07-16 18:45:48

标签: android alarm wakelock android-wake-lock

我正在创建一个闹钟并使用WakeLock在睡着时唤醒手机。

我在我的Activity中使用此代码作为唤醒锁:

library(data.table)#v1.9.5+
setDT(fitchRatings)[, 
 .SD[which.min(as.numeric(levels(LongTermTransformed))[LongTermTransformed])],
              by = .(Country, Year)]

但是当时间满足时,我的应用程序崩溃了这个日志cat错误:

public void onCreate(Bundle savedInstateState) {
    super.onCreate(savedInstateState);
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Wake Log");
    mWakeLock.acquire();
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_screen);
}

我已经添加了对AndroidManifest的许可:

java.lang.IllegalArgumentException: Must specify a valid wake lock level.

我以为我已经指定了唤醒锁等级。我该怎么做才能解决这个问题?请帮我。谢谢

2 个答案:

答案 0 :(得分:4)

WindowManager.LaoutParams.FLAG_KEEP_SCREEN_ON不是PowerManager.newWakeLock()的有效标记/级别。你很可能打算这样做:

PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP

答案 1 :(得分:0)

正确的解决方案如下:将ACQUIRE_CAUSES_WAKEUPPARTIAL_WAKE_LOCK结合使用以唤醒设备。然后使用WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON保持屏幕开启。 PARTIAL_WAKE_LOCK 已弃用且仍可使用。下面是一些示例代码,我的服务唤醒设备,启动相应的活动,并使用唤醒锁保持设备运行,直到活动有时间设置必要的标志以保持设备和屏幕打开。

         // start the activity, wake up the screen if required. This would typically
        // be done in an onReceive() method, or in my case it's when a Bluetooth device has been connected.

        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP), "BlueMaxService");
        wakeLock.acquire();
        Intent intent = new Intent(this, EmaxActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                wakeLock.release();
            }
        }, 10000L);

在活动中,我设置了各种标志来打开屏幕,关闭锁定屏幕,并保持屏幕开启。这些标志必须尽早设置,因此最佳位置在onAttachedToWindow()

        @Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}

如果活动已经开始,则需要重新创建,以便可以设置这些标记。这可以通过在recreate()

中调用onNewIntent()来完成
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    recreate();
}