Today am going to write the on Android Flash Light, This Flash light is working only for Camera flash is available in mobile device.
We can declare this user permission in manifest file.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
http://developer.android.com/reference/android/hardware/Camera.Parameters.html
MainActivity.java
flashlight.xml
AndroidManifest.xml
We can declare this user permission in manifest file.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
http://developer.android.com/reference/android/hardware/Camera.Parameters.html
MainActivity.java
- public class FlashlightActivity extends Activity {private Camera camera;private ToggleButton button;private final Context context = this;@Overrideprotected void onStop() {super.onStop();if (camera != null) {camera.release();}}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.flashlight);button = (ToggleButton) findViewById(R.id.togglebutton);final PackageManager pm = context.getPackageManager();if (!isCameraSupported(pm)) {AlertDialog alertDialog = new AlertDialog.Builder(context).create();alertDialog.setTitle("No Camera");alertDialog.setMessage("The present device's doesn't support camera.");alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {public void onClick(final DialogInterface dialog, final int which) {Log.e("err", "The device's doesn't support camera.");}});alertDialog.show();}camera = Camera.open();}public void onToggleClicked(View view) {PackageManager pm = context.getPackageManager();final Parameters p = camera.getParameters();if (isFlashSupported(pm)) {boolean on = ((ToggleButton) view).isChecked();if (on) {Log.i("info", "torch is turn on!");p.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(p);camera.startPreview();} else {Log.i("info", "torch is turn off!");p.setFlashMode(Parameters.FLASH_MODE_OFF);camera.setParameters(p);camera.stopPreview();}} else {button.setChecked(false);AlertDialog alertDialog = new AlertDialog.Builder(context).create();alertDialog.setTitle("No Camera Flash");alertDialog.setMessage("The device's camera doesn't support flash.");alertDialog.setButton(RESULT_OK, "OK", new DialogInterface.OnClickListener() {public void onClick(final DialogInterface dialog, final int which) {Log.e("err", "The device's camera doesn't support flash.");}});alertDialog.show();}}private boolean isFlashSupported(PackageManager packageManager) {// if device support camera flash?if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {return true;}return false;}private boolean isCameraSupported(PackageManager packageManager) {// if device support camera?if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {return true;}return false;}}
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/relativeLayout1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <ToggleButton
- android:id="@+id/togglebutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:gravity="center"
- android:onClick="onToggleClicked"
- android:textOff="Turn Flashlight on"
- android:textOn="Turn Flashlight off" />
- </RelativeLayout>
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-feature android:name="android.hardware.camera" />
No comments:
Post a Comment