How to open device flashlight in Android N?

本小妞迷上赌 提交于 2019-12-11 05:27:43

问题


I'm trying to make a simple button that will turn on/off the device flashlight. I don't understand why android.hardware.camera is obsolete. What do I have to do in order to make my code working on all devices and also ones with older version of Android?

This is my code:

if (IsFlashlightOn)
{
  if (getPackageManager().hasSystemFeature(
        PackageManager.FEATURE_CAMERA_FLASH)) 
  {
    cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);
    cam.startPreview();
  }
  else
  {
    try
    {
      cam.stopPreview();
      cam.release();
      cam = null;
    }
    catch (Exception ex)
    {
      // Ignore the exception
    }
  }
}

回答1:


There is a mistake in the logic of your code. It's not related to any specific Android version. You are checking if the device has camera flashlight and then, turns it on. In the else block you are turning the camera flashlight off in the case when a device has no camera flashlight what will never happen if your device actually has a flashlight.

I think the code should look like below. It will toggle flashlight (turn it on, when it's turned off and turn it off when it's turned on).

boolean isFlashlightOn = false;
boolean deviceHasCameraFlash = getPackageManager().hasSystemFeature(
                                 PackageManager.FEATURE_CAMERA_FLASH);

if(deviceHasCameraFlash) {
  Camera camera = Camera.open();
  Camera.Parameters parameters = camera.getParameters();

  if(isFlashlightOn) { 
    // turn the flashlight off
    parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(parameters);
    camera.stopPreview();
    isFlashlightOn = false;
  } else {
    // turn the flashlight on
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
    camera.startPreview();
    isFlashlightOn = true;
  }
}

I couldn't test this code right now, but I think it should work, you should get the general idea now and adjust it to your purposes.

To avoid warnings in the IDE and Static Code Analysis tools, you need to add @SuppressWarnings("deprecation") annotation to the deprecated code. We need to keep it in order to have backward compatibility with older Android versions.

If you want to handle Camera on both new and old Android versions, you should prepare the separate code for these versions.

According to the documentation:

We recommend using the new android.hardware.camera2 API for new applications.

It means you should do it in the following way:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  // code for lollipop devices or newer
} else {
  // code for pre-lollipop devices
}

Code for toggling flashlight with the new API would look as follows:

private void toggleFlashLight(boolean isFlashlightOn) {
  CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
  camManager.setTorchMode(cameraId, isFlashlightOn);
}

Moreover, check out these StackOverflow threads:

  • Android camera android.hardware.Camera deprecated
  • Android Turn on/off Camera Flash Programatically with Camera2

They may be helpful while dealing with your issue.

Regards



来源:https://stackoverflow.com/questions/40443836/how-to-open-device-flashlight-in-android-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!