Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of flashlight by Android

Shulou Source: shulou.com Published: 2022-06-02 11:32:12 09月24日 Update

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!

Tags: Flashlight flashlight variable type article function object status event value interest at the same time camera more friends knowledge editing industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information macOS MariaDB Linux