如何在 Android Studio 上以编程方式检查相机闪光灯是否打开?

问题描述 投票:0回答:1

我正在制作一个手电筒应用程序。该应用程序可以选择从后置摄像头闪光灯切换到前置摄像头闪光灯。问题是,当手电筒打开时,如果我将后置摄像头切换到前置,前置摄像头会闪烁不到一秒钟,然后关闭。有时这不会发生。所以,这是一个错误。我希望应用程序检查切换相机后手电筒是否保持打开状态。我想我必须使用火炬回调。但Google的文档描述性不够好,难以理解。而且,SO上对此没有太多讨论。所以,我不知道如何使用火炬回调。 package com.danielfernandzz.flashlight; import android.content.Context; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CameraAccessException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.Switch; public class MainActivity extends AppCompatActivity { public boolean switchState = false; Button switchbutton; String cameraId; CameraManager camManager; Switch frontToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); switchbutton = (Button) findViewById(R.id.Switch); camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); frontToggle = (Switch) findViewById(R.id.frontToggle); try{cameraId = camManager.getCameraIdList()[0];} catch (CameraAccessException ex){finish();} } public void switched(View v){ if(!switchState){ switchState = true; switchbutton.setText("ON"); try {camManager.setTorchMode(cameraId,true);} catch (CameraAccessException ex) {finish();} } else{ switchState = false; switchbutton.setText("OFF"); try {camManager.setTorchMode(cameraId,false);} catch (CameraAccessException ex) {finish();} } } public void frontFlash(View z){ if (frontToggle.isChecked()){ if(switchState) { try {camManager.setTorchMode(cameraId, false);} catch (CameraAccessException ex) {finish();} } try{cameraId = camManager.getCameraIdList()[1];} catch (CameraAccessException ex){finish();} if(switchState) { try {camManager.setTorchMode(cameraId, true);} catch (CameraAccessException ex) {finish();} } //I want to add the callback to take place here } else { if(switchState) { try {camManager.setTorchMode(cameraId, false);} catch (CameraAccessException ex) {finish();} } try{cameraId = camManager.getCameraIdList()[0];} catch (CameraAccessException ex){finish();} if(switchState) { try {camManager.setTorchMode(cameraId, true);} catch (CameraAccessException ex) {finish();} } //And here } } }


android callback camera flashlight camera-flash
1个回答
0
投票
参见 
http://developer.android.com/reference/android/content/pm/PackageManager.html

© www.soinside.com 2019 - 2024. All rights reserved.