Android 监听 ACTION_SCREEN_OFF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11346958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:06:22  来源:igfitidea点击:

Listening for ACTION_SCREEN_OFF

android

提问by Peter

I'm trying to start a service that runs in the background that is listening for ACTION_SCREEN_OFFand when it finds ACTION_SCREEN_OFF, starts my activity.

我正在尝试启动在后台运行的服务,该服务正在侦听,ACTION_SCREEN_OFF并在找到ACTION_SCREEN_OFF时启动我的活动。

I read somewhere you need to create a BroadcastReceiverbecause putting it in the manifest XML doesn't work. However I have no idea where to get started after much searching.

我在某处读到你需要创建一个BroadcastReceiver因为将它放在清单 XML 中不起作用。但是,经过大量搜索后,我不知道从哪里开始。

回答by DragonWork

You cannot declare ACTION_SCREEN_ONand ACTION_SCREEN_OFFin the AndroidManifest.xml. You are only allowed to catch them while your activity is running.

您不能在AndroidManifest.xml 中声明ACTION_SCREEN_ON和。您只能在活动进行时捕捉它们。ACTION_SCREEN_OFF

Here's an example.

这是一个例子。

The BroadcastReceiver:

广播接收器

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(final Context context, final 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;
        }
    }

}

The Activity:

活动

public class ExampleActivity extends Activity {

    private BroadcastReceiver mReceiver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // initialize receiver
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
        // your code
    }

    @Override
    protected void onPause() {
        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            Log.e("MYAPP", "SCREEN TURNED OFF");
        } else {
            // this is when onPause() is called when the screen state has not changed
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // only when screen turns on
        if (!ScreenReceiver.wasScreenOn) {
            // this is when onResume() is called due to a screen state change
            Log.e("MYAPP", "SCREEN TURNED ON");
        } else {
            // this is when onResume() is called when the screen state has not changed
        }
    }

    @Override
    protected void onDestroy() {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
        super.onDestroy();
    }

}

You could probably solve your question by listening to these events from a Service.

您可能可以通过从Service.