简体   繁体   中英

Can't perform action on locking/turning off screen using onPause

I've implemented something similar to Android notification of screen off/on , but it's not working the way it should. All I want to do is stop music when the screen is turned off. I created a screen action class like this

public class ScreenAction extends BroadcastReceiver {
public static boolean wasScreenOn = true;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // DO WHATEVER YOU NEED TO DO HERE
        wasScreenOn = false;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // AND DO WHATEVER YOU NEED TO DO HERE
        wasScreenOn = true;
    }
}

}

Then, in my main activities on create I have this

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenAction();
    registerReceiver(mReceiver, filter);

In my main activities onPause, I have something like this:

 public void onPause() {
    super.onPause();
    if (ScreenAction.wasScreenOn) {
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.pcmouseclick1);
        mp.setVolume(.1f, .1f);
        mp.start();
        if (buttonState) {
            mServ.reduceVolume();
        }
    }
}

I found this from an online source, but I am having issues. It seems that the screen state is always set as true, and I'm not sure how to change this.

How do I utilize this ScreenAction class to turn off music in on Pause ONLY when the user has locked the screen? I feel like I am missing something in the onPause because in onCreate I link to the class.

@Override
protected void onPause() {
    // when the screen is about to turn off
    // or when user is switching to another application

    super.onPause();
}

@Override
protected void onResume() {
    // only when screen turns on
    // or when user returns to application


    super.onResume();
}

Here you can see the entire lifecycle of an Activity in android: Activity Lifecycle

Maybe you could also start and stop your music directly from the receiver:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // Pause music player
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // Resume music player
    }
}

You're checking ScreenAction.wasScreenOn in onPause , but that's happening before the BroadcastReceiver is called to notify you that the screen is being turned off. So at that point, ScreenAction.wasScreenOn is still true, and then it's set to false, but onPause has already run, so your music never gets paused.

To solve this, you should take the action directly in response to the screen turning off in the BroadcastReceiver . If you need to interact with your UI, consider a solution like LiveData as an abstraction so that you're not reliant on your Activity being paused at the exact moment the screen is turned off (consider also that the onPause solution wouldn't work if your Activity weren't currently visible).

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