android 手电筒demo

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)