简体   繁体   中英

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...

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...

确保您请求许可

<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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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