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>

更多相关文章

  1. android正确使用killProcess完全退出应用
  2. android 之Dialog对话框(简易版)
  3. 带确定和取消的
  4. android (back)返回键监听
  5. android webview 保存cookie
  6. android二维码xzing使用
  7. android弹出对话框大全
  8. Android(安卓)Activity界面切换添加动画特效
  9. android完全退出应用程序

随机推荐

  1. 用户注册表单以及选择器权重与上下文选择
  2. box-sizing的作用+伪类选择器的参数 an+b
  3. CSS盒模型常用属性,单位,选择器
  4. 大气红色新闻手机端
  5. HTML5 初识
  6. 9.26作业-仿站php.cn手机端
  7. 1.模态框 2.flex布局 3.grid属性
  8. 云服务器的操作系统一般怎么选?选错了可以
  9. HTML伪类、盒子模型学习与应用
  10. Linux-04压缩文件操作