监听ACTION_SCREEN_OFF [英] Listening for ACTION_SCREEN_OFF

查看:96
本文介绍了监听ACTION_SCREEN_OFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动一个运行在被监听 ACTION_SCREEN_OFF 键,当它找到 ACTION_SCREEN_OFF ,开始我的活动。

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

我读的地方,你需要创建一个的BroadcastReceiver 的,因为把它放在清单的XML不起作用。不过,我不知道在哪里一番搜索后即可开始使用。

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

推荐答案

您不能声明 ACTION_SCREEN_ON ACTION_SCREEN_OFF 的中的的Andr​​oidManifest.xml 的。 您只被允许,而你的活动运行赶上他们。

You cannot declare ACTION_SCREEN_ON and ACTION_SCREEN_OFF in the AndroidManifest.xml. You are only allowed to catch them while your activity is running.

下面是一个例子。

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

}

活动的:

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.

这篇关于监听ACTION_SCREEN_OFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆