手电筒应用程序未在 kitkat 设备中关闭。完美地处理棉花糖

亚当

这是我的手电筒应用程序代码,它在我的牛轧糖和棉花糖设备上完美运行。但是当我在kitkat设备上测试它时,手电筒完美地打开,但没有关闭。我在这段代码中做错了什么,你能帮我找到它吗..我没有在网上找到任何解决方案。请帮忙。

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {


    Button btnSwitch;
    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    Camera.Parameters params;
    MediaPlayer mp;
    private static final String TAG = MainActivity.class.getSimpleName();
    private Camera mCamera;
    private Camera.Parameters parameters;
    private CameraManager camManager;
    private Context context;


    ImageView imageFlashlight;
    private static final int CAMERA_REQUEST = 50;
    private boolean flashLightStatus = false;

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

        imageFlashlight = (ImageView) findViewById(R.id.imageFlashlight);



        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.ic_launcher);

        // First check if device is supporting flashlight or not
        hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);


        final boolean hasCameraFlash = getPackageManager().
                hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        boolean isEnabled = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED;

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.ic_launcher);

        // get the camera


        // displaying button image


        imageFlashlight.setEnabled(isEnabled);

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST);



        imageFlashlight.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                if (hasCameraFlash) {

                    if (flashLightStatus) {
                        turnOffFlash();
                    } else

                        turnOnFlash();
                } else {
                    Toast.makeText(MainActivity.this, "No flash available on your device",
                            Toast.LENGTH_SHORT).show();
                }
            }


        });


    }



    @TargetApi(Build.VERSION_CODES.M)
    private void turnOnFlash() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

            try {
                String cameraId = cameraManager.getCameraIdList()[0];
                cameraManager.setTorchMode(cameraId, true);
                flashLightStatus = true;
                imageFlashlight.setImageResource(R.drawable.poweron);
            } catch (CameraAccessException e) {
            }
        } else {


            imageFlashlight.setImageResource(R.drawable.poweron);

            getCamera();
            params = camera.getParameters();
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;

        }
    }

    // Get the camera
    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera Error. ", e.getMessage());
            }
        }
    }


    @TargetApi(Build.VERSION_CODES.M)
    private void turnOffFlash() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

            try {
                String cameraId = cameraManager.getCameraIdList()[0];
                cameraManager.setTorchMode(cameraId, false);
                flashLightStatus = false;
                imageFlashlight.setImageResource(R.drawable.poweroff);
            } catch (CameraAccessException e) {
            }
        } else {
getCamera();
            if (flashLightStatus) {
                if (isFlashOn = true) {
                    return;
                }

                params = camera.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(params);
                camera.stopPreview();
                isFlashOn = false;
            }
        }


    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CAMERA_REQUEST:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    imageFlashlight.setEnabled(true);
                } else {
                    Toast.makeText(MainActivity.this, "Permission Denied for the Camera", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
Pavneet_Singh

问题在于,flashLightStatus因为您没有将它设置在方法的else一部分turnOnFlashturnOffFlash然后再设置在内部

if (flashLightStatus) {

永远不会是真的,因此控制永远不会达到关灯的程度,所以这样做

@TargetApi(Build.VERSION_CODES.M)
private void turnOnFlash() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            String cameraId = cameraManager.getCameraIdList()[0];
            cameraManager.setTorchMode(cameraId, true);
            imageFlashlight.setImageResource(R.drawable.poweron);
        } catch (CameraAccessException e) {
        }
    } else {


        imageFlashlight.setImageResource(R.drawable.poweron);

        getCamera();
        params = camera.getParameters();
        params = camera.getParameters();
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();

    }
       isFlashOn = true;
       flashLightStatus = true;
}

并且还使用

if (!isFlashOn) {
     return;
}

代替

if (isFlashOn = true) {
    return;
}

当闪光灯不亮时返回控制

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

手电筒(手电筒)正在打开但没有关闭

设备屏幕关闭时如何打开手电筒?

如何在旧设备中打开手电筒

在QRcodeScanner React native中打开/关闭手电筒

如何快速打开和关闭手电筒?

在Android上打开/关闭相机手电筒

Android Studio:手电筒在关闭时崩溃

在Android中以不同的模式打开手电筒

Android片段中的手电筒-SurfaceView

Prolog 中的桥牌手电筒拼图

在KitKat中调用但未在棉花糖中调用的WebView中的startActionMode(在文本选择中)

如何在Android N中打开设备手电筒?

在所有 Android 设备中控制手电筒

火炬/手电筒应用程序(不建议使用android.hardware.camera)

应用程序在后台时,iPhone手电筒无法工作

使用相机api 2的Android手电筒应用程序

Kitkat应用程序是否将在Lollipop版本中运行

在Android中打开不支持同时闪光的手电筒和相机

如何在Tokbox中的手电筒上设置相机灯?

在Android模拟器中模拟LED手电筒?

在手电筒中反转整数类型矩阵时出现错误

在手电筒中,如何在训练时间内修复预训练的嵌入?

在QR码阅读器中启用手电筒

用阵列中的手电筒执行莫尔斯电码

当在Android Studio中按下按钮时,应用会从用户处获取二进制输入并闪烁手电筒以传输输入

我无法在 API <=22(使用 Android Studio 和 Unity)的设备中启动手电筒。为什么?

应用程序在新设备(棒棒糖)上显示错误,但在我的旧设备(kitkat)上运行正常

如何仅在Unity3d中使用C#打开/关闭android手电筒

如何在Android中的以下Kitkat设备上应用渐变