How to realize the function of flashlight by Android
This article mainly introduces Android how to achieve flashlight function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
/ / 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 variables of type Button TextView textView = null; / / create variables of type TextView Camera camera = null; / / create variables of type Camera Parameters params = null; / / create variables of type Parameters Boolean flag = true / / create a variable of type Boolean, which is used to determine whether @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_light); button = (Button) findViewById (R.id.button) / / get Button object textView = (TextView) findViewById (R.id.textView) / / get TextView object button.setOnClickListener (new OnClickListener () {/ / Click event @ Override public 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 ("are you sure you want to quit?") .setIcon (R.drawable.ic_launcher) .setPositiveButton ("OK", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface arg0, int arg1) {/ / TODO Auto-generated method stub LightActivity.this.finish () System.exit (0) ) .setNegativeButton ("Cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface arg0) Int arg1) {/ / TODO Auto-generated method stub}}) .show () }} BlueOcean flashlight Hello world! Settings flashlight current status: turn on flashlight current state: turn off flashlight turn off flashlight Author:BlueOcean Thank you for reading this article carefully. I hope the article "how to realize the flashlight function of Android" shared by the editor is helpful to everyone. At the same time, I also hope that you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!