码迷,mamicode.com
首页 > 移动开发 > 详细

android 手电筒demo

时间:2014-12-09 21:16:00      阅读:260      评论:0      收藏:0      [点我收藏+]

标签: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

标签:des   android   style   blog   http   io   ar   color   os   

原文地址:http://www.cnblogs.com/sphere/p/4153960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!