【问题标题】:catching screen off before is too late之前关闭屏幕为时已晚
【发布时间】:2011-12-16 16:41:40
【问题描述】:

我有一个活动,它的 onPause 必须做一些工作,但不是在屏幕关闭时。 我已经为ACTION_SCREEN_OFF 意图注册了接收者,理论上,这个和应用程序级别的静态标志应该可以解决问题,但是......它不起作用,因为活动上的onPause 回调在之前被调用接收者可以得到它的意图。 即:logcat*ting* 按下空闲按钮时,我可以先看到onPause 跟踪,然后是onReceive。 此时,设置静态标志已经不是很重要了……

是否有可能在活动的onPause 时间知道屏幕已关闭?

提前致谢
L.

【问题讨论】:

  • 肯定会调用 onPause 然后屏幕熄灭。这有什么问题?
  • 如果您每次调用 onPause 时都需要做一些工作,那就什么都不做。不幸的是,只有在屏幕没有关闭的情况下,我才必须在 onPause 中完成这项工作。所以问题是:我如何及时意识到屏幕消失了? BroadcastReceiver不好:太慢了……
  • 如果屏幕打开,您需要做什么工作才能严格执行?当显示器从用户收回时调用 onPause,隐含地(形象地和有效地)表示屏幕“关闭”。我不明白如果屏幕关闭来完成这项工作为什么很重要。
  • 与工作无关,但我喜欢这个标题。太……戏剧化了。 ;-)
  • 你可以覆盖空闲键,然后在你的应用程序中你可以做任何你需要做的事情,然后如果你需要使用这个方法使屏幕变暗:stackoverflow.com/questions/6756768/turn-off-screen-on-android

标签: android screen screen-off


【解决方案1】:

我有同样的问题,接收器不是要走的路

安卓开发者

http://developer.android.com/reference/android/content/BroadcastReceiver.html

"注意:如果在 Activity.onResume() 实现中注册接收器,则应在 Activity.onPause() 中取消注册。(暂停时不会收到意图,这将减少不必要的系统开销) . 不要在 Activity.onSaveInstanceState() 中取消注册,因为如果用户移回历史堆栈,则不会调用它。”

而是在你的 onPause 方法中创建一个电源管理器 [我从这个链接得到它]

how to find out screen status on an android device?

public void onPause(){

   PowerManager powermanager =(PowerManager)global.getSystemService(Context.POWER_SERVICE); 
   if (powermanager.isScreenOn()){
        screen_off_beforePause = false;
            //your code here
   }else{
    screen_off_beforePause = true;
   }
}

public void onResume() {
   if (screen_off_beforePause){
    Log.e(TAG + ".onResume()", "screen was off before onPause");
   }else{
    Log.d(TAG + ".onResume()", "screen was not off before onPause");
    //your code here
   }

 //resetting
 screen_off_beforePause = false;    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多