最近在研究开启安卓手电筒功能,总结了下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" />
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
布局文件
<?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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
具体实现
/**
* Created by 猴子搬来的救兵Castiel
* 博客地址:http://blog.youkuaiyun.com/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;
}
}
}
http://blog.eastmoney.com/j1550024695909052/blog_570169545.html
http://blog.eastmoney.com/j1550024695909052/blog_570170372.html
http://blog.eastmoney.com/j1550024695909052/blog_570274542.html
http://blog.eastmoney.com/j1550024695909052/blog_570282244.html
http://www.wang1314.com/doc/topic-1459190-1.html
http://www.wang1314.com/doc/topic-1459198-1.html
http://www.wang1314.com/doc/topic-1460029-1.html
http://www.wang1314.com/doc/topic-1460083-1.html
http://guba.com/show-3719985-1.html
http://guba.com/show-3720420-1.html
http://guba.com/show-3720468-1.html
http://guba.com/show-3720509-1.html
http://bbs.clzg.cn/thread-7534941-1-1.html
http://bbs.clzg.cn/thread-7534945-1-1.html
http://bbs.clzg.cn/thread-7536599-1-1.html
http://bbs.ttx.cn/read-htm-tid-8266937.html
http://bbs.ttx.cn/read-htm-tid-8266940.html
http://bbs.ttx.cn/read-htm-tid-8267496.html
http://www.818u.com/beijing/qihuo/a39797737.html
http://www.818u.com/beijing/qihuo/a39798187.html
http://www.818u.com/beijing/qihuo/a39809274.html
http://www.kejiqi.com/baoxiangongsi/14452596x.html
http://www.siteloop.net/article/355445.html
http://www.siteloop.net/article/355446.html
http://www.230la.com/news/show-97142.html
http://www.lxwei.com/thread-110356-1-1.html
http://www.lxwei.com/thread-110357-1-1.html
http://www.cnxixian.com/gqinfo/details.asp?id=40008
http://www.kejiqi.com/baoxiangongsi/14452715x.html
http://www.kejiqi.com/baoxiangongsi/14466762x.html
http://www.togv.net/tiyupeixun/j5gfy7gb7gazj1aztyo0.htm
http://www.togv.net/tiyupeixun/ugz2wfjifj8f6p6o9yn5.htm
http://www.togv.net/tiyupeixun/dceb5h07000crwai3r17.htm
http://beijing.baixing.com/licaifuwu/a1067014832.html
http://beijing.baixing.com/licaifuwu/a1067015218.html
http://bbs.henan100.com/thread-1081279-1-1.html
http://bbs.henan100.com/thread-1081281-1-1.html
http://bbs.jrhot.com/thread-1505997-1-1.html
http://bbs.jrhot.com/thread-1506010-1-1.html
http://bbs.jrhot.com/thread-1506432-1-1.html
http://bbs.jrhot.com/thread-1506454-1-1.html
http://bbs.jrhot.com/thread-1506513-1-1.html
http://www.siteloop.net/article/355722.html
http://www.siteloop.net/article/355723.html
http://www.230la.com/news/show-97219.html
http://www.230la.com/news/show-97220.html
http://www.lxwei.com/thread-111970-1-1.html
http://www.lxwei.com/thread-111971-1-1.html
http://bbs.qz.xmfish.com/read-htm-tid-2608009-ds-1.html
http://bbs.qz.xmfish.com/read-htm-tid-2608011-ds-1.html
http://bbs.qz.xmfish.com/read-htm-tid-2608121-ds-1.html
http://bbs.qz.xmfish.com/read-htm-tid-2608127-ds-1.html
http://bbs.qz.xmfish.com/read-htm-tid-2608131-ds-1.html
http://cn.51tie.com/touzidanbaobaoxian/11022980x.html
http://cn.51tie.com/touzidanbaobaoxian/11022988x.html
http://beijing.jinti.com/licai/22240598.htm
http://beijing.jinti.com/licai/22240618.htm
http://tieba.baidu.com/p/4866428130
http://mt.sohu.com/20161118/n473515618.shtml
http://mt.sohu.com/20161118/n473514838.shtml
http://mt.sohu.com/20161118/n473516292.shtml
http://mt.sohu.com/20161118/n473544926.shtml
http://mt.sohu.com/20161118/n473544922.shtml
http://bbs.henan100.com/thread-1075263-1-1.html
http://bbs.henan100.com/thread-1075269-1-1.html
http://bbs.henan100.com/thread-1077677-1-1.html
http://bbs.henan100.com/thread-1077686-1-1.html
http://bbs.henan100.com/thread-1077962-1-1.html
http://cn.51tie.com/touzidanbaobaoxian/11013233x.html
http://cn.51tie.com/touzidanbaobaoxian/11013247x.html
http://cn.51tie.com/touzidanbaobaoxian/11018075x.html
http://beijing.jinti.com/licai/22226159.htm
http://www.lxwei.com/thread-111503-1-1.html
http://bbs.henan100.com/thread-1066752-1-1.html
http://bbs.henan100.com/thread-1067208-1-1.html
http://bbs.henan100.com/thread-1067387-1-1.html
http://www.818u.com/beijing/qihuo/a39719297.html
http://www.818u.com/beijing/qihuo/a39719346.html
http://www.818u.com/beijing/qihuo/a39736848.html
http://www.kejiqi.com/baoxiangongsi/14368928x.html
http://www.818u.com/beijing/qihuo/a39684040.html
http://www.818u.com/beijing/qihuo/a39684092.html
http://www.hainei.org/thread-6609638-1-1.html
http://www.hainei.org/thread-6609639-1-1.html
http://www.lxwei.com/thread-109540-1-1.html
http://guba.com/show-3716204-1.html
http://guba.com/show-3716208-1.html
http://guba.com/show-3716437-1.html
http://guba.com/show-3716443-1.html
http://www.kejiqi.com/baoxiangongsi/14330147x.html
http://www.kejiqi.com/baoxiangongsi/14341996x.html
http://www.kejiqi.com/baoxiangongsi/14293826x.html
http://www.kejiqi.com/baoxiangongsi/14313397x.html
http://www.kejiqi.com/baoxiangongsi/14313919x.html
http://www.kejiqi.com/baoxiangongsi/14314106x.html
http://www.kejiqi.com/baoxiangongsi/14314352x.html
http://www.hainei.org/thread-6601991-1-1.html
http://www.hainei.org/thread-6601997-1-1.html
http://www.lxwei.com/thread-108596-1-1.html
http://www.lxwei.com/thread-108598-1-1.html
http://www.cnxixian.com/gqinfo/details.asp?id=39992
http://www.cnxixian.com/gqinfo/details.asp?id=39993