Android6.0 已经抛弃了Camer 相关的API,改用新的API接口CamerManager,下面给出使用的简单实例

package com.inper.duqiang.slashlight;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton; public class MainActivity extends Activity { private CameraManager manager;
private Camera camera = null;
private Camera.Parameters parameters = null;
public static boolean kaiguan = true; // 定义开关状态,状态为false,打开状态,状态为true,关闭状态 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState); manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try {
String [] camerList = manager.getCameraIdList();
for (String str:camerList
) {
Log.d("List",str);
}
} catch (CameraAccessException e) {
Log.e("error",e.getMessage());
}
Button open_btn = (Button) findViewById(R.id.open_btn); open_btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
if (isLOLLIPOP()) {
try {
manager.setTorchMode("0", true);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
}); Button close_btn = (Button) findViewById(R.id.close_btn);
close_btn.setOnClickListener(closeOnClickListener); ToggleButton toggle_btn = (ToggleButton) findViewById(R.id.toggle_btn);
toggle_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
manager.setTorchMode("1", isChecked);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
});
} private View.OnClickListener closeOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isLOLLIPOP()) {
try {
manager.setTorchMode("0", false);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
};
/**
* 判断Android系统版本是否 >= LOLLIPOP(API21)
*
* @return boolean
*/
private boolean isLOLLIPOP() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return true;
} else {
return false;
}
}
}

Layout的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.inper.duqiang.slashlight.MainActivity"> <ToggleButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toggle_btn"/>
<Button
android:layout_below="@+id/toggle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开闪光灯"
android:id="@+id/open_btn"/>
<Button
android:layout_below="@+id/toggle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/open_btn"
android:text="关闭闪光灯"
android:id="@+id/close_btn"/>
</RelativeLayout>

Anf文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inper.duqiang.slashlight"> <!-- 打开Camera的权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" /> <!-- 开启闪光灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" /> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

Android 6.0 闪光灯的使用的更多相关文章

  1. Android 6.0 动态权限申请注意事项

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 And ...

  2. Android 6.0 API

    Android 6.0 (M) 为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的 API. 着手开发 要着手开发 Android 6.0 应用,您必须先获得 Android SDK,然后使 ...

  3. Android 6.0动态权限(转)

    转自:http://blog.csdn.net/uana_777/article/details/54136255 Part One 权限区分 Android 6.0 为了保护用户隐私,将一些权限的申 ...

  4. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

  5. Android权限管理之RxPermission解决Android 6.0 适配问题

    前言: 上篇重点学习了Android 6.0的运行时权限,今天还是围绕着Android 6.0权限适配来总结学习,这里主要介绍一下我们公司解决Android 6.0权限适配的方案:RxJava+RxP ...

  6. Android权限管理之Android 6.0运行时权限及解决办法

    前言: 今天还是围绕着最近面试的一个热门话题Android 6.0权限适配来总结学习,其实Android 6.0权限适配我们公司是在今年5月份才开始做,算是比较晚的吧,不过现在Android 6.0以 ...

  7. Android 5.0 到 Android 6.0 + 的深坑之一 之 .so 动态库的适配

    (原创:http://www.cnblogs.com/linguanh) 目录: 前序 一,问题描述 二,为何会如此"无情"? 三,目前存在该问题的知名SDK 四,解决方案,1 对 ...

  8. Android 7.0 Nougat牛轧糖 发布啦

    Android 7.0 Nougat牛轧糖 发布啦 Android 7.0 Nougat 牛轧糖于本月发布了. 从官方blog里可以了解到这个版本的新特性. Android 7.0 从2016年8月正 ...

  9. Android 6.0 运行时权限处理完全解析

    一.概述 随着Android 6.0发布以及普及,我们开发者所要应对的主要就是新版本SDK带来的一些变化,首先关注的就是权限机制的变化.对于6.0的几个主要的变化,查看查看官网的这篇文章http:// ...

随机推荐

  1. 基尔霍夫矩阵题目泛做(AD第二轮)

    题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...

  2. 复习了下自定义style的使用

    一.为什么要自定义style 这是样式与控件本身脱离的一种方式.style就像html中的css,只负责自定义样式.View控件在layout中就只负责声明自己就可以了. 就像这样: 首先在style ...

  3. WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数

    上一篇对插件函数之动作(Action)函数做了下介绍,这篇在介绍下过滤器(Filters). 过滤器是一类函数,WordPress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定运行( ...

  4. [Python shelve模块Error]bsddb.db.DBPageNotFoundError: (-30986, 'DB_PAGE_NOTFOUND: Requested page not found')

    在用scrapy抓数据,用shelve保存时出现了这个Error,目标是储存一串unicode字符串组成的列表,exception代码是tempbase['joke']=joke_list,测试只要j ...

  5. python代码风格规范

    类注释模板: :: class AnotherClass: """ 类注释 """ def method(self, arg1, arg2, ...

  6. AutoCompleteTextView 与sqlite绑定实现记住用户输入的内容并自动提示

    把用户输入的内容保存到数据库表中,然后用户输入时,进行模糊查询并把查询结果附到AutoCompleteTextView中. 1:activity_main.xml <LinearLayout x ...

  7. C# LinkButton 带参数的OnCommand事件的写法

    前台: <asp:TemplateField HeaderText ="操作"> <HeaderStyle HorizontalAlign ="Cent ...

  8. 25045操作标准子程序集41.C

    /* ;程 序 最 后 修 改 时 间 0-4-3 23:43 ;软 件 标 题:25045操作标准子程序集41 ;软 件 说 明:25045 I2C 串行EEPROM 驱动 ;___________ ...

  9. android WebView, WebChromeClient和WebViewClient加载网页基本用法

    WebView, WebChromeClient和WebViewClient加载网页基本用法 webview是android中的浏览器控件,在一些手机应用中常会用到b/s模式去开发应用,这时webvi ...

  10. POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

    参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...