Sunday , November 28 2021

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);

setContentView()R.Layout.activity_main);

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

.hasSystemFeature()package manager.FEATURE_CAMERA_FLASH);

If ()!isFlashAvailable) {{

showNoFlashError());

}

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

try {{

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

} Hold ()CameraAccessException I) {{

I.Printstacktress());

}

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.Satellite()“Oops!”);

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) {{

End());

}

});

Cautious.Display());

}

public Zero Switchflight()Boolean Event) {{

try {{

mCameraManager.setTorchMode()mCameraId, Event);

} Hold ()CameraAccessException I) {{

I.Printstacktress());

}

}

}


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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