旋转 Android 设备时 Phonegap 崩溃
所需的行为是这样的:
在旋转时重建布局 - 以适应新的屏幕尺寸。重建是使用 JQuery 和数学完成的 - 而不是 CSS。
脚本状态必须保持相同,还运行计时器等。这些应该像什么都没有发生一样继续。
为了实现这一点,我做了这个:
在清单中的所有活动中添加了 android:configChanges="orientation|keyboardHidden" ,以自己处理方向更改(注意:我不针对某些 API,所以我认为它默认到 API 1。这意味着不需要将 screenSize 添加到 configChanges)。
添加了方向更改的侦听器:
window.onorientationchange = function() { var 方向 = window.orientation; 开关(方向){ 案例0: 更新缩放(); 休息;
<前><代码>案例 90: 更新缩放(); 休息; 情况-90: 更新缩放(); 休息; } }
updateScaling 仅调整布局。
有时这会起作用,但有时设备会崩溃(模拟器以及真实设备)。在模拟器的日志上我看到这个警告:
警告/ActivityManager(73):启动超时已过期,放弃唤醒锁!
我进行了研究,发现在活动中添加更长的 loadUrlTimeoutValue 可能会有所帮助,而且我添加了 super.setIntegerProperty("loadUrlTimeoutValue", 60000);到主要活动,但正如预期的那样也没有帮助。
我发现很多人抱怨 PhoneGap 的轮换问题。也许框架在这方面只是有缺陷?我还用 Apple 设备进行了测试,在那里我什至不可能捕获旋转事件 - iPhone 模拟器甚至没有改变方向,iPad 改变了它,但事件处理程序没有被调用,布局保持相同的大小。我尝试使用我发现的解决方法,将侦听器附加到文档。但这也没有帮助。
旋转设备时是否有完全可靠的方法来重建布局?如果没有,我可以在使用某个应用程序初始化后禁用旋转(这意味着,我不想修复为纵向或横向 - 我希望应用程序在使用当前应用程序初始化后禁用旋转更改)。有可靠的方法来做到这一点吗?
谢谢
The desired behaviour is this:
On rotation the layout is reconstructed - in order to fit new screen size. Reconstruction is done with JQuery and maths - not CSS.
The scripts state must keep the same, also running timers, etc. These should continue like nothing happened.
In order to achieve this I made this:
Added android:configChanges="orientation|keyboardHidden" to all activities in the manifest, to handle orientation change myself (note: I'm not targeting certain API, so I suppose it defaults to API 1. This means that add screenSize to configChanges is not necessary).
Added a listener for orientationchange:
window.onorientationchange = function() {
var orientation = window.orientation;
switch(orientation) {
case 0:
updateScaling();
break;case 90: updateScaling(); break; case -90: updateScaling(); break; } }
updateScaling only adjusts the layout.
And this works sometimes, but sometimes the device crashes (emulator as well as real device). On the emulator's log I see this warning:
WARN/ActivityManager(73): Launch timeout has expired, giving up wake lock!
I made research and found add a longer loadUrlTimeoutValue in the activity could help, and well, I added super.setIntegerProperty("loadUrlTimeoutValue", 60000); to the main activity but as expected doesn't help either.
I found many people complaining about rotation problems with PhoneGap. Is maybe the Framework just buggy for this aspect? I tested also with Apple devices, and there I was not even possible to catch the rotating event - the iPhone emulator didn't even change the orientation, the iPad changed it but the event handler is not called and the layout keeps the same size. I tried using a workaround I found attaching a listener to document. But it didn't help either.
Is there a fully reliable way to reconstruct the layout when rotating the device? If not, can I disable rotation after the app being initialized with a certain one (that means, I don't want to fix to portrait or landscape - I want the application disables rotation changes after it was initialized using the current one). Is there a reliable way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定这是否解决了崩溃问题,或者我“在路上”做了其他事情。
但我添加了一个可靠工作的侦听器 - 至少在 Android 上:
它总是被调用并传递正确的宽度。据我记得它也适用于 screen.width
在 updateScaling 中我计算一个scalingFactor并调整我的元素。
我删除了 window.onorientationchange。
注意:可能是在崩溃时我在清单中使用了 android:configChanges="keyboardHidden" 而不是 android:configChanges="orientation|keyboardHidden" 。 AFAIK 这意味着系统重建了活动,这可以解释超时。但在那种情况下我不知道为什么 super.setIntegerProperty("loadUrlTimeoutValue", 60000);没有帮助。
结论
$(window).resize 与 android:configChanges="orientation|keyboardHidden" 结合使用对我有用。
I'm not quite sure if this fixed the crash problem, or something else I did "on the way".
But I added a listener which works reliably - at least on Android:
It's always called and passes correct width. As far as I remember it also works with screen.width
In updateScaling I calculate a scalingFactor and adjust my elements.
I removed window.onorientationchange.
Note: It might be that at the point of time of the crashes I had android:configChanges="keyboardHidden" instead of android:configChanges="orientation|keyboardHidden" in the manifest. AFAIK this means the system reconstruct the activity, this could explain the timeout. But in that case I don't know why super.setIntegerProperty("loadUrlTimeoutValue", 60000); didn't help.
Conclusion
$(window).resize in combination with android:configChanges="orientation|keyboardHidden" is working for me.