PARTIAL_WAKE_LOCK和ON_AFTER_RELEASE不会打开屏幕

时间:2013-09-07 15:49:10

标签: android android-wake-lock

我创建了一个BroadcastReceiver,用于从AlarmManager打开屏幕。这是我OnReceive的代码。

@Override
public void onReceive(Context arg0, Intent arg1) {
    Log.i("", "OnReceive");

    // Initializes the PowerManager.
    PowerManager powerManager = (PowerManager) arg0
            .getSystemService(Context.POWER_SERVICE);

    // Wakes device.
    WakeLock wakeLock = powerManager.newWakeLock(
            PowerManager.FULL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.ON_AFTER_RELEASE, "Stretching");
    wakeLock.acquire();
    wakeLock.release();
    wakeLock = null;

    // Calls activity.
    Intent intent = new Intent(arg0, TrainingActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(intent);
}

出现此问题是因为FULL_WAKE_LOCK已弃用,因此我尝试使用PARTIAL_WAKE_LOCK,但屏幕无法启动。我应该用什么呢? 我搜索了这个How to turn screen on during partial wake lock,但它也在使用FULL_WAKE_LOCK

2 个答案:

答案 0 :(得分:0)

使用已弃用的FULL_WAKE_LOCK(并准备好让您的应用程序在将来可能不再有效),或者不要编写应用程序。 PARTIAL_WAKE_LOCK专门无法打开屏幕。

答案 1 :(得分:0)

不幸的是,FULL_WAKE_LOCK已被弃用,有利于节省电池电量。

唯一合理的解决方法是测试Android版本,如果版本早于KitKat,即Lollipop和up(直到KitKat FULL_WAKE_LOCK一直工作正常但在API LEVEL 17中已弃用),您可以使用FULL_WAKE_LOCK,否则使用PARTIAL_WAKE_LOCK。像这样:

    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(
        (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ? PowerManager.PARTIAL_WAKE_LOCK : PowerManager.FULL_WAKE_LOCK) |
        PowerManager.ACQUIRE_CAUSES_WAKEUP |
        PowerManager.ON_AFTER_RELEASE, "Some Tag");

    wakeLock.acquire(10000);

希望有所帮助!