Android Flash Light Example?

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

  1. public class FlashlightActivity extends Activity {
    private Camera camera;
    private ToggleButton button;
    private final Context context = this; 
    @Override
    protected void onStop() {
    super.onStop();
    if (camera != null) {
    camera.release();
    }
    }
    @Override
    public 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;
    }
    } 




flashlight.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/relativeLayout1"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent" >

  6.     <ToggleButton
  7.         android:id="@+id/togglebutton"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_marginTop="20dp"
  11.         android:gravity="center"
  12.         android:onClick="onToggleClicked"
  13.         android:textOff="Turn Flashlight on"
  14.         android:textOn="Turn Flashlight off" />

  15. </RelativeLayout>
  16.  



AndroidManifest.xml

  1.  <uses-permission android:name="android.permission.CAMERA" />
  2.  <uses-feature android:name="android.hardware.camera" />



No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...