Turn on Flashlight Android – Building a Torch App – Android Coding by DMTechnolab

The package The trap.Simplified coding.Mytorch;

Import Android.Content.Context;

Import Android.Content.Dialog interface;

Import Android.Content.in the evening.package manager;

Import Android.The hardware.Camera 2.CameraAccessException;

Import Android.The hardware.Camera 2.Camera man;

Import Android..Bundle;

Import Android.Support.v7.App.AlertDialog;

Import Android.Support.v7.App.AppCompatActivity;

Import Android.Widget.Compound button;

Import Android.Widget.Toggle button;

public class Main activity Is spread AppCompatActivity {{

Private Camera man mCameraManager;

Private String mCameraId;

Private Toggle button Toggle button;

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


Boolean isFlashAvailable = = getApplicationContext()).getPackageManager())

.hasSystemFeature()package manager.FEATURE_CAMERA_FLASH);

If ()!isFlashAvailable) {{



mCameraManager = = ()Camera man) getSystemService()Context.CAMERA_SERVICE);

try {{

mCameraId = = mCameraManager.getCameraIdList())[0];

} Hold ()CameraAccessException I) {{



Toggle button = = findViewById()R.Id.Toggle button);

Toggle button.setOnCheckedChangeListener()New Compound button.OnCheckedChangeListener()) {{

@ Override

public Zero onCheckedChanged()Compound button Button view, Boolean is checked) {{

Switchflight()is checked);




public Zero showNoFlashError()) {{

AlertDialog Cautious = = New AlertDialog.Builder()this)

.To create());


Cautious.Setmessage()“Flash is not available on this device …”);

Cautious.set button()Dialog interface.BUTTON_POSITIVE, “OK”, New Dialog interface.OnClickListener()) {{

public Zero on click()Dialog interface conversation, int Which which) {{






public Zero Switchflight()Boolean Event) {{

try {{

mCameraManager.setTorchMode()mCameraId, Event);

} Hold ()CameraAccessException I) {{





Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *