首页 > 代码库 > Android 6.0 闪光灯的使用
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>
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 闪光灯的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。