Android Flashlight Case

Source: Internet
Author: User

Recently in the study of Android Development, wrote a flashlight case, share!

activity.xmlpackage com.fq.flashlight;import android.app.activity;import  Android.app.alertdialog;import android.content.dialoginterface;import android.hardware.camera;import  android.hardware.camera.parameters;import android.os.bundle;import android.view.view;import  android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView; public class lightactivity extends activity {button button = null;// Create a variable of type button textview textview = null;//Create a variable of type TextView camera camera = null;// Create a variable of camera type parameters params = null;//create a variable of type parameters boolean flag = true;// Creates a Boolean variable that is used to make an open decision @overrideprotected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_light);button =  (Button) Findviewbyid (R.id.button);//Gets the button object Textview =&nbSP; (TextView)  findviewbyid (R.id.textview);//Get TextView object Button.setonclicklistener (new  Onclicklistener ()  { //Click event @overridepublic void onclick (VIEW V)  {if (flag) {camera  = camera.open ();     //get camera params = camera.getparameters (); Params.setflashmode (Parameters.flash_mode_torch); camera.setparameters (params); Camera.startpreview (); Textview.settext (r.string.alert_on); Button.settext (r.string.button_off); flag = false;} Else{params.setflashmode (Parameters.flash_mode_off); camera.setparameters (params); Camera.stoppreview (); Camera.release (); Textview.settext (R.string.alert_off); Button.settext (r.string.button_on); flag = true ;}}});} Public void onbackpressed () {//super.onbackpressed (); System.out.println ("Sdsfs"); New alertdialog.builder (this). Settitle ("OK to exit?"). "). SetIcon (R.drawable.ic_launcher). Setpositivebutton (" OK ",  new dialoginterface.onclicklistener ()  {@Overridepublic  void&Nbsp;onclick (DIALOGINTERFACE ARG0, INT ARG1)  {// todo auto-generated method  stublightactivity.this.finish (); System.exit (0);}}). Setnegativebutton ("Cancel",  new dialoginterface.onclicklistener ()  {@Overridepublic  void  onclick (DIALOGINTERFACE ARG0, INT ARG1)  {// TODO Auto-generated  Method stub}}). Show ();}}
<!--strings.xml--><?xml version= "1.0"  encoding= "Utf-8 "? ><resources>    <string name=" App_name ">blueocean flashlight </string>     <string name= "Hello_world" >Hello world!</string>     <string name= "Action_settings" >settings</string>   <string  name= "alert_on" > Flashlight current status: Turn on </string><string name= "Alert_off" > Flashlight current status: Off </string ><string name= "button_on" > Turn on Flashlight </string><string name= "Button_off" > Turn off Flashlight </ String><string name= "Author" >AUTHOR:BLUEOCEAN</STRING></RESOURCES> 
<!--layout.xml--><?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns: Android= "Http://schemas.android.com/apk/res/android"     android:layout_width= "match_parent "    android:layout_height=" Match_parent " >    <textview         android:id= "@+id/textview"          android:layout_width= "Wrap_content"         android:layout_ height= "Wrap_content"         android:layout_alignparenttop= "true"         android:layout_centerhorizontal= "true"          android:layout_margintop= "34DP"         android : text= "@string/alert_off"         android:textsize= "22SP"  />     <button        android:id= "@+id/button"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_below= "@+id /textview "        android:layout_centerhorizontal=" true "         android:layout_margintop= "26DP"          android:text= "@string/button_on"  />    <TextView         android:id= "@+id/textview2"         android: Layout_width= "Wrap_content"         android:layout_height= "wrap_content "        android:layout_alignright="     "@+id/textview"     &nBsp;android:layout_centervertical= "true"         android:text= "@string/ Author "        android:textappearance="? android:attr/ Textappearancesmall " /></RelativeLayout>
<!-- manifest.xml--><?xml version= "1.0"  encoding= "Utf-8"? ><manifest xmlns: Android= "Http://schemas.android.com/apk/res/android"     package= "Com.fq.flashlight"      android:versioncode= "1"     android:versionname= "1.0"  >     <uses-sdk        android:minsdkversion= "8"          android:targetsdkversion= " />    <" Uses-permission android:name= "Android.permission.CAMERA"/><uses-permission android:name= " Android.permission.FLASHLIGHT "/>        <application         android:allowbackup= "true"          android:icon= "@drawable/ic_launcher"         android:label= "@string/app _name "  &nbsP;     android:theme= "@style/apptheme"  >         <activity            android:name= " . Mainactivity "            android:label=" @string/ App_name " >        </activity>                 <activity             android:name= ". Lightactivity "            android:label=" @string/ App_name " >            <intent-filter >                <action  android:name= "Android.intent.action.MAIN" &NBSP;/&GT;&NBSP;&Nbsp;              <category  Android:name= "Android.intent.category.LAUNCHER"  />             </intent-filter>        </activity >    </application></manifest>


This article is from the "Blueocean" blog, make sure to keep this source http://bluocean.blog.51cto.com/8199259/1554839

Android Flashlight Case

Related Article

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.