Java Can';t使用onPause在锁定/关闭屏幕上执行操作

Java Can';t使用onPause在锁定/关闭屏幕上执行操作,java,android,android-lifecycle,game-development,Java,Android,Android Lifecycle,Game Development,我已经实现了类似的东西,但它没有按应有的方式工作。我只想在屏幕关闭时停止音乐。我创建了一个像这样的屏幕动作类 public class ScreenAction extends BroadcastReceiver { public static boolean wasScreenOn = true; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equal

我已经实现了类似的东西,但它没有按应有的方式工作。我只想在屏幕关闭时停止音乐。我创建了一个像这样的屏幕动作类

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;
    }
}

}
然后,在我关于创建的主要活动中,我有

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenAction();
    registerReceiver(mReceiver, filter);
在Pause上的主要活动中,我有如下内容:

 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();
        }
    }
}
我从网上找到了这个,但我有问题。屏幕状态似乎总是设置为true,我不知道如何更改

如何利用ScreenAction类仅在用户锁定屏幕时在暂停时关闭音乐?我觉得我在onPause中遗漏了一些东西,因为在onCreate中我链接到了类

@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();
}
在这里,您可以看到android中活动的整个生命周期:

在这里,您可以看到android中活动的整个生命周期:

也许您也可以直接从接收器启动和停止音乐:

@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
    }
}

也许您也可以直接从接收器启动和停止音乐:

@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
    }
}

您正在
onPause
中检查
ScreenAction.wasScreenOn
,但这是在调用
BroadcastReceiver
通知您屏幕已关闭之前发生的。所以在这一点上,
ScreenAction.wasScreenOn
仍然是真的,然后它被设置为false,但是
onPause
已经运行,所以你的音乐永远不会暂停


要解决此问题,您应该在
BroadcastReceiver
中关闭屏幕时直接执行此操作。如果你需要与你的UI交互,考虑一个解决方案,比如LiviaDATA作为一个抽象,这样你就不依赖于你的活动在屏幕关闭的瞬间暂停(也考虑到 OnPosip解决方案如果你的活动目前不可见的话)就不起作用了。。所以在这一点上,
ScreenAction.wasScreenOn
仍然是真的,然后它被设置为false,但是
onPause
已经运行,所以你的音乐永远不会暂停


要解决此问题,您应该在
BroadcastReceiver
中关闭屏幕时直接执行此操作。如果你需要与你的UI交互,考虑一个解决方案,比如LiviaDATA作为抽象,这样你就不依赖于在屏幕关闭的时候暂停你的活动(也考虑到OpPosith解决方案如果你的活动目前不可见的话就不起作用)。“仅当用户锁定屏幕时暂停”这不回答如何操作“仅当用户锁定屏幕时暂停”