Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- ImageButton btnSwitch;
- private Camera camera;
- private boolean isFlashOn, hasFlash;
- private MediaPlayer mp;
- private Camera.Parameters params;
- protected void onCreate(Bundle b) {
- super.onCreate(b);
- setContentView(R.layout.activity_main);
- // flash switch button
- btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
- // First check if device is supporting flashlight or not
- hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
- if (!hasFlash) {
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("Error")
- .setMessage("Sorry, your device doesn't support flash light!")
- .setCancelable(false)
- .setNegativeButton
- ("Close", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- finish();
- }
- }
- );
- AlertDialog alert = builder.create();
- alert.show();
- return;
- }
- // get the camera
- getCamera();
- // displaying button image
- toggleButtonImage();
- // Switch button click event to toggle flash on/off
- btnSwitch.setOnClickListener
- (new View.OnClickListener() {
- public void onClick(View v) {
- if (isFlashOn) {
- // turn off flash
- turnOffFlash();
- } else {
- // turn on flash
- turnOnFlash();
- }
- }
- }
- );
- }
- protected void getCamera() {
- if (camera == null) {
- try {
- camera = Camera.open();
- params = camera.getParameters();
- } catch (RuntimeException e) {
- Log.e("Failed to Open. Error: ", e.getMessage());
- }
- }
- }
- protected void turnOnFlash() {
- if (!isFlashOn) {
- if (camera == null || params == null) {
- return;
- }
- // play sound
- playSound();
- params = camera.getParameters();
- params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
- camera.setParameters(params);
- camera.startPreview();
- isFlashOn = true;
- // changing button/switch image
- toggleButtonImage();
- }
- }
- protected void turnOffFlash() {
- if (isFlashOn) {
- if (camera == null || params == null) {
- return;
- }
- // play sound
- playSound();
- params = camera.getParameters();
- params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
- camera.setParameters(params);
- camera.stopPreview();
- isFlashOn = false;
- // changing button/switch image
- toggleButtonImage();
- }
- }
- protected void playSound() {
- if (isFlashOn) {
- mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
- } else {
- mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
- }
- mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- public void onCompletion(MediaPlayer mp) {
- // TODO Auto-generated method stub
- mp.release();
- }
- });
- mp.start();
- }
- protected void toggleButtonImage() {
- if (isFlashOn) {
- btnSwitch.setImageResource(R.drawable.btn_switch_on);
- } else {
- btnSwitch.setImageResource(R.drawable.btn_switch_off);
- }
- }
- protected void onDestroy() {
- super.onDestroy();
- }
- protected void onPause() {
- super.onPause();
- // on pause turn off the flash
- turnOffFlash();
- }
- protected void onRestart() {
- super.onRestart();
- }
- protected void onResume() {
- super.onResume();
- // on resume turn on the flash
- if (hasFlash)
- turnOnFlash();
- }
- protected void onStart() {
- super.onStart();
- // on starting the app get the camera params
- getCamera();
- }
- protected void onStop() {
- super.onStop();
- // on stop release the camera
- if (camera != null) {
- camera.release();
- camera = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement