之前关闭屏幕就太晚了

发布于 2024-12-21 17:32:27 字数 371 浏览 1 评论 0原文

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

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

提前致谢
L。

I've an activity that, on its onPause has to do some work but not when the screen is turned off.
I've already registered the receiver for the ACTION_SCREEN_OFF intent and, theoretically, this and a static flag at application level should do the trick but... It doesn't work because the onPause callback on the activity is invoked BEFORE the receiver can get its Intent.
That is: logcat*ting* while push down the idle button, i can see the onPause trace first and the onReceive after.
At this point, setting up the static flag is not very important...

Any possibilities to know at activity's onPause time that the screen has turned off?

Thanks in advance
L.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-12-28 17:32:27

出路

我遇到了同样的问题,接收器不是Android 开发人员的

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

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

相反,在 onPause 方法中创建一个电源管理器[我从这个链接得到它]

如何查看 Android 设备的屏幕状态?

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

I had the same problem and a receiver is not the way to go

android developer

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

"Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack."

Instead create a power manager in your onPause method [I got it from this link]

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;    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文