简体   繁体   English

WakeLock没有释放且屏幕没有关闭

[英]WakeLock not releasing and Screen isn't turning off

I've using alarm manager to call an activity and I'm using the wake locker class onRecive() to wake the phone and then calling WakeLocker.release() after the Activity is over but the screen still stays on... 我正在使用警报管理器来调用活动,并且正在使用唤醒储物柜类 onRecive()来唤醒电话,然后在活动结束后仍然调用WakeLocker.release(),但屏幕仍保持打开状态...

Receive.class: 接收类:

public class MyScheduledReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    WakeLocker.acquire(context);

Activity.class 活动类

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        WakeLocker.release();
        finish();
        }

I've put it in the onPause(), onStop() everywhere... the thing won't release and the screen won't turn off automatically after my app closes... 我把它放到了onPause(),onStop()的任何地方...在我的应用程序关闭后,东西不会释放,屏幕也不会自动关闭...

确保您请求许可

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

Try this 尝试这个

PowerManager pm;
PowerManager.WakeLock wakeLock;

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
                            "x2_wakelook");


wakeLock.acquire();
wakeLock.release();

You are starting the wakelock in a broadcastreceiver and stopping it in an activity. 您正在广播接收机中启动唤醒锁,并在活动中将其停止。 You are referencing 2 different instances of a wakelock. 您正在引用2个不同的唤醒锁实例。 You should start the activity from the onreceive and in onresume acquire the wake lock, then still release in the onpause if that is where you want it to happen. 您应该从onreceive开始活动,并在onresume中获取唤醒锁,如果您希望发生这种情况,则仍应在onpause中释放它。

You should never start anything that is supposed to be around for awhile within a broadcastreceiver, because the receiver is destroyed as soon as possible. 永远不要在广播接收器中启动任何可能会存在的东西,因为接收器会被尽快销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM