Created
March 17, 2016 10:11
-
-
Save rasikag/d7990b16324a14a75f52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FlashLightActivity extends AppCompatActivity { | |
private ImageButton mSwich; | |
private boolean isFlashOn; | |
private boolean isDeviceHasFlas; | |
private MediaPlayer mMediaPlayer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_flash_light); | |
mSwich = (ImageButton) findViewById(R.id.btnSwitch); | |
isDeviceHasFlas = getApplicationContext().getPackageManager() | |
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); | |
if (!isDeviceHasFlas) { | |
AlertDialog alert = new AlertDialog.Builder(FlashLightActivity.this) | |
.create(); | |
alert.setTitle("Error"); | |
alert.setMessage("Sorry, your device doesn't support flash light!"); | |
alert.setButton("OK", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
finish(); | |
} | |
}); | |
alert.show(); | |
return; | |
} | |
//getCamera(); | |
isFlashOn = false; | |
toggleButtonImage(); | |
mSwich.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent i = new Intent(getApplicationContext(),FlashLightBackGroundService.class); | |
if (isFlashOn) { | |
stopService(i); | |
toggleButtonImage(); | |
isFlashOn = true; | |
} else { | |
startService(i); | |
toggleButtonImage(); | |
isFlashOn = true; | |
} | |
} | |
}); | |
} | |
// private void getCamera() { | |
// if (mCamera == null) { | |
// try { | |
// mCamera = Camera.open(); | |
// mParams = mCamera.getParameters(); | |
// } catch (RuntimeException e) { | |
// Log.e("Failed to Open. Error: ", e.getMessage()); | |
// } | |
// } | |
// } | |
// private void turnOnFlash() { | |
// | |
// if (!isFlashOn) { | |
// if (mCamera == null || mParams == null) { | |
// return; | |
// } | |
// mParams = mCamera.getParameters(); | |
// mParams.setFlashMode(mParams.FLASH_MODE_TORCH); | |
// mCamera.setParameters(mParams); | |
// mCamera.startPreview(); | |
// isFlashOn = true; | |
// | |
// toggleButtonImage(); | |
// } | |
// | |
// } | |
// private void turnOffFlash() { | |
// if (isFlashOn) { | |
// if (mCamera == null || mParams == null) { | |
// return; | |
// } | |
// mParams = mCamera.getParameters(); | |
// mParams.setFlashMode(mParams.FLASH_MODE_OFF); | |
// mCamera.setParameters(mParams); | |
// mCamera.stopPreview(); | |
// isFlashOn = false; | |
// | |
// toggleButtonImage(); | |
// } | |
// } | |
private void toggleButtonImage() { | |
if (isFlashOn) { | |
mSwich.setImageResource(R.drawable.btn_switch_on); | |
playSound(); | |
} else { | |
mSwich.setImageResource(R.drawable.btn_switch_off); | |
playSound(); | |
} | |
} | |
private void playSound() { | |
if (isFlashOn) { | |
mMediaPlayer = MediaPlayer.create(FlashLightActivity.this, R.raw.light_switch_off); | |
} else { | |
mMediaPlayer = MediaPlayer.create(FlashLightActivity.this, R.raw.light_switch_on); | |
} | |
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
mp.release(); | |
} | |
}); | |
mMediaPlayer.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment