春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述Android 6.0 闪光灯的使用相关的知识,希望能为你提供帮助。
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>
【Android 6.0 闪光灯的使用】 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开发者网址导航
- Android下载更新代码
- android 常用URI
- Android MVP + 娉涘瀷锛屽疄鐜颁簡鍙嬪ソVP浜や簰鍙夾ctivity娼滃湪鐨勫唴瀛樻硠闇茬殑浼樺寲
- 在 Android studio 中 配置Gradle 做到 “根据命令行提示符生成指定versionCode, versionName,指定apk的打包输出路径”
- Android问题-新电脑新系统WIN764位上安装简版本的XE8提示“Unit not found: 'System'”
- react native android 开发,基础配置笔记。
- Android Studio一直build一直refreshing一直buiding gradle project into的终极解决办法
- Android Studio 如何使用jni