且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PhoneGap 相机重新启动应用程序

更新时间:2022-01-31 22:38:30

这个问题实际上与 Phonegap 无关.这也是原生 Android 应用的常见问题.

This problem isn't actually about Phonegap. It's a common issue on native android apps too.

这是因为当相机被触发时,android Activity 进入后台(onStop 状态),等待相机拍照.然后 GC 会在相机动作结束之前杀死活动以释放内存,当相机完成时,您的活动已经死亡.这就是应用重新启动的原因.

It occurs because when the camera is triggered, the android activity goes background (onStop state), waiting for the camera to take the picture. Then the GC comes and kills the activity to free memory before the conclusion of camera action, and when the camera is done your activity has already died. That is why the app is restarted.

它在 Android 生命周期文档 (http://developer.android.com/reference/android/app/Activity.html ):

It's on Android Lifecycle docs (http://developer.android.com/reference/android/app/Activity.html ):

如果一个活动被另一个活动完全遮挡,它就会停止.它仍然保留所有状态和成员信息,但是,它不再对用户可见,因此它的窗口是隐藏的,并且在其他地方需要内存时经常被系统杀死.

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

访问媒体库或其他导致您的活动进入后台的资源也会发生同样的情况.Phonegap(现在是 Cordova)团队已经在努力改进这一点(https://issues.apache.org/jira/browse/CB-14).

The same occurs by acessing the media gallery or other resources that causes your activity to go background. Phonegap (now Cordova) team is already working to improve this ( https://issues.apache.org/jira/browse/CB-14 ).

我们公司遇到了这个问题,解决方案是在本地开发一个 Phonegap 插件来使用我们定制的相机,然后我们的活动永远不会进入 onStop 状态.按照 http://developer.android.com/guide/topics/上的 android API 说明进行操作media/camera.html#custom-camera 并尝试一下.

We had this problem in our company and the solution was to natively develop a Phonegap plugin to use our customized camera, then our activity never go to onStop state. Follow the android API instructions on http://developer.android.com/guide/topics/media/camera.html#custom-camera and try it too.

见!

编辑 1:

我们提交了一个名为 Foreground Camera Plugin 的 Google Code 项目,该项目修复了 Android 相机重启 Phonegap 应用程序的问题.关于如何使用它也有一些指导.请参阅:http://code.google.com/p/foreground-camera-plugin/

We submited a Google Code project named Foreground Camera Plugin that fixes the problem of Android Camera restarting Phonegap applications. There is some orientation on how to use it too. Please see: http://code.google.com/p/foreground-camera-plugin/

编辑 2:

由于此问题也发生在图库中,因此我们提交了另一个名为 Foreground Gallery 插件 的 Google 代码项目,该项目适用于 Cordova 并修复了此问题.请参阅:http://code.google.com/p/foreground-gallery-plugin/

Since this problem happens with gallery too, we submited another Google Code project named Foreground Gallery Plugin that works with Cordova and fixes this issue. Please see: http://code.google.com/p/foreground-gallery-plugin/