Android flashlight demo and androiddemo

Source: Internet
Author: User

Android flashlight demo and androiddemo

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 Oid. 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) // determine whether the device supports the flashlight {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 ("Exit program "). setMessage ("are you sure you want to exit the flashlight * ^_^ *"). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {tryClosedLight (); finish ();}}). setNegativeButton ("cancel", 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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.