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. C#通过文件路径截取对应的文件夹路径

      try { OpenFileDialog openFileDialog = new OpenFileDialog(); string str = comboBox_hexFilePath.Text ...

  2. jar打包命令

    jar查看jar命令的使用 1. jar cvf 生成jar包的完整名称 要生成的class文件所在目录以及名称 jar -cvf aa.jar *.* 2.将jar包放到环境变量中,可以不用把cla ...

  3. phantomjs初入门

    对DOM操作,而调试过程必不可少,对于那些微乎其微的方法,总显得余力不足.在这里PhantomJS就就行了很好的实现. PhantomJS是一个拥有JavaScript API的无界面WebKit 正 ...

  4. Asset Catalog Creator Free 生成程序内图标的软件

    Asset Catalog Creator Free  生成程序内图标的软件

  5. abiword rtf 解析

    目前为止,代码跟进,知道是这个地方进行文件解析的 T_Error IE_Imp_RTF::importFile(const char * szFilename)

  6. Tk::Table

    <pre name="code" class="python"># DESCRIPTION # Tk::Table is an all-perl w ...

  7. Spring使用小结2

    之前做过不少spring想过知识点内容的摘录, Spring框架的特点.模块组成.优缺点 spring相关的bean管理想过知识点及依赖注入方式 今天说下近端时间中项目中遇到的相关印象比较深的知识点 ...

  8. sourceforge软件下载方式

    访问http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/ 根据软件名称在文件列表中查找

  9. unix c 07

    进程的结束函数 (exit._Exit)    exit 并不是立即退出,退出前执行 用atexit/on_exit函数 注册的函数.    exit(int status)中的status可以用 w ...

  10. web应用,我们需要了解什么?

        对于前端开发来说,web应用我们并不陌生.今天想要讨论一下,在开发一个web应用的时候,我们需要一些基本的知识储备.我们知道,一个web应用脱离不了(request)请求和响应(respons ...