Android flashlight source code

Source: Internet
Author: User






Public class MainActivity extends Activity {private Button bu1; private Camera camera; private Parameters parameters; private static boolean kaiguan = true; public int back = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); LinearLayout container = (LinearLayout) findViewById (R. id. acc); new AdView (this, cont Ainer ). displayAd (); bu1 = (Button) findViewById (R. id. bu1); bu1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub if (kaiguan) {bu1.setBackgroundResource (R. drawable. on); camera = Camera. open (); parameters = camera. getParameters (); parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); camera. setParameters (parameters); kaiguan = false ;} Else {bu1.setBackgroundResource (R. drawable. off); parameters. setFlashMode (Parameters. FLASH_MODE_OFF); camera. setParameters (parameters); kaiguan = true; camera. release () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. menu. add (0, 1, "about"); menu. add (1, 2, "quit"); return super. onCreateOptionsMenu (menu);} publ Ic boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case 1: AlertDialog. builder B = new Builder (MainActivity. this); B. setTitle ("about"); B. setMessage ("Kaka shoudian makes the most concise application: Someone"); B. setCancelable (true); B. setNegativeButton ("OK", new DialogInterface. onClickListener () {public void onClick (DialogInterface d, int which) {d. dismiss () ;}}); B. show (); break; case 2: Myback (); break;} r Eturn super. onOptionsItemSelected (item);} public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {back ++; switch (back) {case 1: Toast. makeText (MainActivity. this, "Quit Kaka again", Toast. LENGTH_LONG ). show (); break; case 2: back = 0; // initialize the back value Myback (); break;} return true; // set it to false to invalidate the back, true indicates no expiration} else {return super. onKeyDown (keyCode, event) ;}} public v Oid Myback () {if (kaiguan) {// when the switch is off, MainActivity. this. finish (); android. OS. process. killProcess (android. OS. process. myPid (); // closes the process} else if (! Kaiguan) {// camera when the switch is on. release (); MainActivity. this. finish (); android. OS. process. killProcess (android. OS. process. myPid (); // closes the process kaiguan = true; // avoids this problem. After the switch is enabled, the program exits. If the switch is not enabled again, the program exits }}}
<! -- Open the permission of Camera --> <uses-permission android: name = "android. permission. CAMERA "/> <uses-feature android: name =" android. hardware. camera "/> <uses-feature android: name =" android. hardware. autofocus "/> <! -- Enable the FLASHLIGHT permission --> <uses-permission android: name = "android. permission. FLASHLIGHT"/> <uses-permission android: name = "android. permission. DISABLE_KEYGUARD"/>


This article is from the "IT growth" blog, please be sure to keep this source http://xyo123.blog.51cto.com/6369437/1294982

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.