[英]How to turn on Front flashlight(not the rear one) of a android device
I developed a simple application to turn ON/OFF flashlight.我开发了一个简单的应用程序来打开/关闭手电筒。 But I couldn't find a way to turn on front flashlight of a device(if available).
但我找不到打开设备前置手电筒的方法(如果有)。
Code for availability check:可用性检查代码:
boolean hasCameraFlash = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
My code for turn ON is:我的打开代码是:
@RequiresApi(api = Build.VERSION_CODES.M)
private void flashLightOn() {
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
} catch (CameraAccessException ignored) {
}
}
Is it possible to turn on front flashlight of device without turning on camera.是否可以在不打开相机的情况下打开设备的前置手电筒。 Any help will be appreciated.
任何帮助将不胜感激。
(Please note that I'm a newcomer to android development) (请注意我是android开发新手)
For Checking availability of flash in the device:检查设备中 flash 的可用性:
You can use the following:您可以使用以下内容:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.如果 flash 可用,则返回 true,否则返回 false。
See:看:
http://developer.android.com/reference/android/content/pm/PackageManager.html for more information. http://developer.android.com/reference/android/content/pm/PackageManager.html了解更多信息。
For turning on/off flashlight:打开/关闭手电筒:
I googled out and got this about android.permission.FLASHLIGHT.我用谷歌搜索得到了关于 android.permission.FLASHLIGHT 的信息。 Android manifests' permission looks promising:
Android 清单的权限看起来很有希望:
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_flashlight"
android:description="@string/permdesc_flashlight" />
Code Snippet to turn on camera flashlight.代码片段打开相机手电筒。
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
Code snippet to turn off camera led light.关闭相机 LED 灯的代码片段。
cam.stopPreview();
cam.release();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.