android 手電筒demo

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   ar   color   os   

package com.sphere.flashlight;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.pm.FeatureInfo;import android.content.pm.PackageManager;import android.hardware.Camera;import android.os.Bundle;import android.view.KeyEvent;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;import android.widget.ToggleButton;public class MainActivity extends Activity {        private ToggleButton button ;        private Camera camera;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (ToggleButton)findViewById(R.id.lightButton);        button.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton button, boolean checked) {                if(checked){                    tryOPenLight();                }else {                    tryClosedLight();                }            }        });    }        private void tryOPenLight(){        PackageManager pm= this.getPackageManager();        FeatureInfo[]  features = pm.getSystemAvailableFeatures();        for(FeatureInfo f : features){             if(PackageManager.FEATURE_CAMERA_FLASH.equals(f.name))   //判斷裝置是否支援閃光燈             {                if ( null == camera )                  {                   camera = Camera.open();                      }                 Camera.Parameters parameters = camera.getParameters();                                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);                     camera.setParameters(parameters);                               camera.startPreview();             }        }        if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){             button.setChecked(false);             Toast.makeText(getBaseContext(), "Sorry.You phone not support flashlight", Toast.LENGTH_SHORT).show();         }    }        private void tryClosedLight(){        if ( camera != null )             {                 camera.stopPreview();                 camera.release();               camera = null;             }    }        @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        switch (keyCode) {        case KeyEvent.KEYCODE_BACK:            AlertDialog.Builder build=new AlertDialog.Builder(this);               build.setTitle("退出程式")               .setMessage("確定要退出手電筒嗎 *^_^*")               .setPositiveButton("確定", new DialogInterface.OnClickListener() {                     @Override                  public void onClick(DialogInterface dialog, int which) {                       tryClosedLight();                    finish();                }               })               .setNegativeButton("取消", new DialogInterface.OnClickListener() {                   @Override                  public void onClick(DialogInterface dialog, int which) {                       // TODO Auto-generated method stub                   }               })               .show();               break;        }        return super.onKeyDown(keyCode, event);    }    @Override    protected void onDestroy() {        tryClosedLight();        super.onDestroy();    }}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sphere.flashlight"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <uses-permission android:name="android.permission.FLASHLIGHT"/>    <uses-permission android:name="android.permission.WAKE_LOCK"/>    <uses-permission android:name="android.permission.CAMERA"/>    <uses-permission android:name="android.hardware.camera"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.sphere.flashlight.MainActivity"            android:screenOrientation="portrait"            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

android 手電筒demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.