简体   繁体   English

屏幕关闭时,时髦的Android活动行为?

[英]Funky android activity behavior when screen turns off?

I'm seeing some interesting activity flow when android turns off the screen and locks the screen... my app goes through the regular flow, onCreate, onStart and onResume. 当android关闭屏幕并锁定屏幕时,我看到一些有趣的活动流程...我的应用程序通过常规流程,onCreate,onStart和onResume。 Then, I let my phone sit there. 然后,我让手机坐在那里。 When the screen goes black, onPause is called. 当屏幕变黑时,会调用onPause。 That's fine. 没关系。 Then 5 seconds later, the app is killed (onStop, onDestroy). 然后5秒钟,应用程序被杀死(onStop,onDestroy)。 That's fine too. 那也没关系。 But immediately after onDestroy is called, onCreate, onStart and onResume are called, restarting the app even though the screen is blank. 但是在调用onDestroy后立即调用onCreate,onStart和onResume,即使屏幕为空,也会重新启动应用程序。 The app has a load time, and it plays a sound when it starts, so it's kind of creepy when the phone you set down 30 seconds ago starts playing sounds. 该应用程序有一个加载时间,并在它启动时播放声音,所以当你30秒前设置的手机开始播放声音时,它会有点令人毛骨悚然。 Why does android kill the app then restart it? 为什么android会杀死应用程序然后重启它? Target is 2.1-update, and the phone is a Samsung Captivate. 目标是2.1更新,手机是三星Captivate。 Anyone else seeing this, and know a way to stop it? 其他人看到这个,并知道一种方法来阻止它?

It may not be what you are seeing, but on my phone, something like this happens because sleep mode is always in one orientation, and if the app was in the other one it gets killed and recreated in the sleep orientation... rather suboptimal if you ask me. 它可能不是你所看到的,但在我的手机上,这样的事情会发生,因为睡眠模式始终处于一个方向,如果应用程序位于另一个方向,它会在睡眠方向被杀死并重新创建...相当欠佳如果你问我。

It sounds to me like you may not have things such as your startup sound tied to sufficiently specific causes. 听起来像你可能没有诸如你的启动声音与足够具体的原因相关的事情。 What happens if you rotate the phone while it's "on" ? 如果您在手机“开启”时旋转手机会怎样? At any rate, you should be able to detect that the screen is not on and not do (or defer) a real startup. 无论如何,您应该能够检测到屏幕未打开而不能(或推迟)真正的启动。

If an implicit orientation change is the culprit, you can change how your application is treated with these - set things to claim your application can change on the fly instead of having to be recreated. 如果隐式方向更改是罪魁祸首,您可以更改应用程序的处理方式 - 设置要求您的应用程序可以动态更改而不必重新创建。

In the activity's manifest, for each application add: 在活动的清单中,为每个应用程序添加:

android:configChanges="orientation|keyboardHidden"

This tells android you'll handle these two cases yourself - although of course you're lying: you won't do shit to handle them, but Android doesn't know that. 这告诉android你自己会处理这两个案例 - 虽然你当然在说谎:你不会做任何事情来处理它们,但Android并不知道这一点。 This works great for apps/games/etc running for example in landscape mode and never change orientation. 这适用于以横向模式运行的应用/游戏/等,并且永远不会改变方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM