Android 6.0 閃光燈的使用

來源:互聯網
上載者:User

標籤:

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.