android:按下主页按钮和电源按钮之间的区别? [英] android: Difference between pressing home button and power button?

查看:44
本文介绍了android:按下主页按钮和电源按钮之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道按电源键和home键的区别吗?在我的应用程序中,我尝试在 Activity 的每个生命周期中放置一个调试语句,结果都是一样的,也就是说,当重新打开 Activity 时,该 Activity 将处于 onPause 状态,然后是 onRestart 然后是 onResume(如果我们按下 Home按钮更早)或当我们通过按下电源按钮再次打开设备时(如果我们更早按下电源按钮).

does anybody know the difference between pressing the power button and the home button? In my app, I've tried putting a debugging statement in every lifecycle of the activity and both result the same, that is, the activity will be onPause and will be onRestart then onResume when the activity is re-opened (if we press home button earlier) or when we turn on the device again by pressing the power button (if we press power button earlier).

具有相同的行为/序列,我希望我的相机应用程序在两种情况下运行相同.但事实并非如此.我使用surfaceview来预览相机.如果按下主页按钮然后重新打开应用程序,它会正常运行.

Having this same behaviour/sequence, I expected my camera app to run the same in both cases. But it's not. I used surfaceview to preview to camera. If the press the home button and then re-open the app, it'll run normally.

但是,如果我按下电源按钮然后再次按下它再次打开它,surfaceView 会冻结,因此预览也会冻结.但是相机实际上仍在工作(如果我按下相机按钮,它仍然会拍摄照片).

BUT, if I press the power button and then press it again to turn it on again, the surfaceView freezes and thus the preview freezes too. But the camera is actually still working (if I press the camera button, itll still capture a photo).

所以我真的很困惑这两者之间的区别...

So I'm really stuck in what's the difference between this two...

经过测试和测试,我发现不同之处在于,当我按下主页按钮时,应用程序将处于onPause(),然后surfaceView将被销毁.另一方面,如果我按下电源按钮,surfaceView 不会被破坏.

After testing and testing, I've found out the difference is that when I press home button, the app will be onPause() and then the surfaceView will be destroyed. On the other hand, if I press the power button, the surfaceView is not destroyed.

仍然对导致这两种不同行为的原因感到困惑......

Still confused on what causing the two different behavior though...

推荐答案

我发现了同样的问题,我相信当按下电源按钮时,设备从横向变为纵向以显示锁定屏幕.在我的例子中,我看到的是 SurfaceChanged 而不是预期的被破坏,但宽度和高度表明变化从横向变为纵向.

I have discovered the same issue and i believe that when the PowerButton is pressed, the device changes from landscape to portrait to display the lock-screen. In my case i see the surfaceChanged instead of the expected destroyed, but the width and height show that the change went from landscape to portrait.

编辑

在我的 Galaxy S2 4.1.2 上,当我按下电源按钮时,设备只会进入onPause"状态,并且表面只会发生变化(从横向变为纵向).当我点击 HomeButton 时,它会转到onStop"并且表面被破坏而没有任何变化.

On my Galaxy S2 with 4.1.2 when i press the PowerButton the device only goes to "onPause" and the surface only changes (from landscape to portrait). When i hit the HomeButton, then it goes to "onStop" and the surface is destroyed without a change.

如果在onPause"中尝试以下操作,您可以确定屏幕是仍然打开还是关闭

If you try the following in "onPause" you can determine whether the screen is still on or off

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();

API 等级 7.

AFAIK 自己在onPause"中调用onStop"是安全的,因为你不能改变真正的生命周期,你只需执行你写到 stop 中的代码(超级方法似乎什么都不做,我检查了活动来源)

AFAIK it is safe to call "onStop" yourself in "onPause" as you cant cause the true lifecycle to change, you just execute the code that you wrote into the stop (the super method seems to do nothing, i checked the Activity source)

这篇关于android:按下主页按钮和电源按钮之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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