【发布时间】:2015-04-12 17:43:09
【问题描述】:
我正在编写一个与用户对话和倾听的应用程序。基本上是一款无需查看或触摸设备即可拨打电话的应用程序(信不信由你,我在市场上没有找到任何设备!)。 这里的重点是我想要:
- 在屏幕熄灭时继续运行
- 当用户切换到另一个活动时停止运行
而且我没有看到一个简单的方法来理解这一点。 我已经调查过了:How to stop a background thread when the screen in android device goes off
但事件的触发顺序错误(来自我的日志):
- onPause:屏幕状态未知
- onStop:屏幕状态未知
-
屏幕事件:关闭
public class ScreenReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // do whatever you need to do here wasScreenOn = ScreenStatus.OFF; Log.d(TAG,"screen event OFF"); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { // and do whatever you need to do here wasScreenOn = ScreenStatus.ON; Log.d(TAG,"screen event ON"); } } }
【问题讨论】:
-
我怀疑 Screen on off 和 Activity 生命周期方法彼此同步运行。