android - 如何临时释放相机?

标签 android camera android-service

我的应用有两个服务将使用相机

Service2 会抛出异常,因为摄像头被Service1

使用

但是我发现一些相机应用,当其他应用打开相机时,它会暂时释放相机,直到其他应用完成

如何让Service1在Service2打开相机时可以释放相机?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent1 = new Intent(getApplicationContext(), Service1.class);
    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(intent1);
    Intent intent2 = new Intent(getApplicationContext(), Service2.class);
    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(intent2);
}

public class Service1 extends Service {
    Camera camera;

    public Service1() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            camera = Camera.open();
        }catch(Exception e){
            Log.e("service1", "open camera error");
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(camera!=null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }
}

public class Service2 extends Service {

    Camera camera;

    public Service2() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            camera = Camera.open();
        }catch(Exception e){
            Log.e("service2", "open camera error");
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(camera!=null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }
}

最佳答案

Rather than puting Camera code in both service class you should create a Camera Util and put your camera code there and check there.. is your camera is on or off..so you don't need to check is your service is running or not..

public boolean isCameraUsebyApp() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (RuntimeException e) {
        return true;
    } finally {
        if (camera != null) camera.release();
    }
    return false;
}

关于android - 如何临时释放相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33751482/

相关文章:

android - 我无法使用 subclipse 提交 .so 库文件

android - 使用改造库获取响应时出错

ios - xCode 创建 UIImagePickerController 并引用它

android - 泄漏金丝雀检测 MediaBrowserServiceCompat 示例应用程序中的内存泄漏

android - IntentService onHandleIntent() 在 onDestroy() 触发后仍在运行

android - 这是即使在低内存压力下也能保持服务 Activity 的正确技巧吗?

android - 如何为公司注册安卓开发者账号?

android - Google Play Store 是否向 "Beta App Update"发送 "Beta Testers"通知?

android - Camera.setPreviewCallback(PreviewCallback)不工作

Android 应用程序与 Nexus 7 不兼容