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