为什么相机需要的onPause(待公布),而不是活动的onStop()方法? [英] Why Camera needs to be released in onPause() rather than onstop() method of Activity?

查看:332
本文介绍了为什么相机需要的onPause(待公布),而不是活动的onStop()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎失去了一些东西,我不明白为什么Android文档中( Android相机文档中的onPause()回调的活动链接)建议释放Camera对象(以及MediaRecorder)?活动仍可能到那个时候可见光和摄像机可能运行preVIEW为什么相机对象将在的onPause()释放而不是当的onStop活动已经隐藏()?据我所知,MediaRecorder对象可以在的onPause停止(),但相机本身没有道理给我。我缺少的是在这里吗?从Android文档code这块低于(其下释放相机标题):

  @覆盖
保护无效的onPause(){
    super.onPause();
    releaseMediaRecorder(); //如果你正在使用MediaRecorder,先将它
    releaseCamera(); //立即释放在相机上暂停事件
}私人无效releaseMediaRecorder(){
    如果(mMediaRecorder!= NULL){
        mMediaRecorder.reset(); //明确记录配置
        mMediaRecorder.release(); //释放记录对象
        mMediaRecorder = NULL;
        mCamera.lock(); //锁摄像头,供以后使用
    }
}


解决方案

根据应用程序生命周期

 暂停
    另一项活动是在前台和具有焦点,但是这一个是
            仍清晰可见。也就是说,另一项活动是可见的在此之上
            之一,该活动是部分透明或不覆盖
            整个屏幕。 (......)

我觉得尽快释放资源的文档遵循的经验法则:的onPause 比早期的onStop

相机在后台窗口需要能源,而用户必须支付给关注弹出。

相机在研究背景当然是更舒适,但对于一个移动电池续航时间更重要。

这截获您的活动可能会用到的摄像头和/或可能需要大量内存的弹出窗口。

在您的方案时,照相机应继续记录在后台相机 - S的生命周期和记录应当由服务控制

I seem to be missing something as I fail to understand why in Android documentation (Android Camera doc. link) it is recommended to release Camera object (as well as MediaRecorder) in onPause() Activity callback? Activity still might be visible by that time and Camera might be running preview so why the Camera object would be released in onPause() rather then onStop() when activity is already hidden? I understand that MediaRecorder object could be stopped in onPause() but Camera itself doesn't make sense to me. What am I missing here? Piece of code from Android documentation is below (its under Releasing the Camera heading):

    @Override
protected void onPause() {
    super.onPause();
    releaseMediaRecorder();       // if you are using MediaRecorder, release it first
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseMediaRecorder(){
    if (mMediaRecorder != null) {
        mMediaRecorder.reset();   // clear recorder configuration
        mMediaRecorder.release(); // release the recorder object
        mMediaRecorder = null;
        mCamera.lock();           // lock camera for later use
    }
}

解决方案

according to application lifecycle

Paused
    Another activity is in the foreground and has focus, but this one is 
            still visible. That is, another activity is visible on top of this 
            one and that activity is partially transparent or doesn't cover the 
            entire screen. (...)

I think the documentation follows the rule of the thumb "release resources as soon as possible": onPause is earlier than onStop.

camera in the background window needs energy while the user has to to pay attentions to the popup.

Camera in the backgroud is of course more comfortable but for a mobile battery life time is more important.

The popup that intercepted you activity might need the camera and/or might need a lot of memory.

In your scenario when the camera should continue recording in the background the camera-s lifecycle and recording should be controlled by a service

这篇关于为什么相机需要的onPause(待公布),而不是活动的onStop()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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