查看: 269|回复: 0

[Android教程] Android开启手电筒功能(完美适配Android4x,5x,6x)

发表于 2018-4-12 08:00:03

最近在研究开启安卓手电筒功能,总结了下Android4x、5x、6x的手电筒开启方法,写成公共方法,分享给大家。

这里写图片描述

添加摄像机和闪光灯权限

    
    
    1234
布局文件


    
具体实现
/**
 * Created by 猴子搬来的救兵Castiel
 * 博客地址:http://blog.csdn.net/mynameishuangshuai
 */public class MainActivity extends AppCompatActivity {
    private CameraManager manager;// 声明CameraManager对象
    private Camera m_Camera = null;// 声明Camera对象

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnOpen = (Button) findViewById(R.id.btn_open);
        Button btnClose = (Button) findViewById(R.id.btn_close);
        manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);        try {
            String[] camerList = manager.getCameraIdList();            for (String str : camerList) {
            }
        } catch (CameraAccessException e) {
            Log.e("error", e.getMessage());
        }

        btnOpen.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View view) {
                lightSwitch(false);
            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View view) {
                lightSwitch(true);
            }
        });
    }    /**
     * 手电筒控制方法
     *
     * @param lightStatus
     * @return
     */
    private void lightSwitch(final boolean lightStatus) {        if (lightStatus) { // 关闭手电筒
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                try {
                    manager.setTorchMode("0", false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {                if (m_Camera != null) {
                    m_Camera.stopPreview();
                    m_Camera.release();
                    m_Camera = null;
                }
            }
        } else { // 打开手电筒
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                try {
                    manager.setTorchMode("0", true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {                final PackageManager pm = getPackageManager();                final FeatureInfo[] features = pm.getSystemAvailableFeatures();                for (final FeatureInfo f : features) {                    if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) { // 判断设备是否支持闪光灯
                        if (null == m_Camera) {
                            m_Camera = Camera.open();
                        }                        final Camera.Parameters parameters = m_Camera.getParameters();
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        m_Camera.setParameters(parameters);
                        m_Camera.startPreview();
                    }
                }
            }
        }
    }    /**
     * 判断Android系统版本是否 >= M(API23)
     */
    private boolean isM() {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {            return true;
        } else {            return false;
        }
    }
}




回复

使用道具 举报