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

时间:2023-02-09 11:19:55

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

Android开启手电筒功能(完美适配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:layout_height="match_parent"
android:orientation="vertical">


<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>

具体实现

/**
* 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;
}
}
}