原文http://blog.csdn.net/mynameishuangshuai
Android4x、5x、6x的手电筒开启方法,写成公共方法,分享给大家。
添加摄像机和闪光灯权限
<uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.autofocus" />布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <Button android:id="@+id/btn_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="打开手电筒" /> <Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭手电筒" /> </LinearLayout>代码:
package com.example.donghe.myapplication; import android.content.Context; import android.content.pm.FeatureInfo; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.camera2.CameraManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity1 extends AppCompatActivity { private CameraManager manager;// 声明CameraManager对象 private Camera m_Camera = null;// 声明Camera对象 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hand_light); Button btnOpen = (Button) findViewById(R.id.btn_open); Button btnClose = (Button) findViewById(R.id.btn_close); manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 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(); } } } } } }