install android studio

How to make Flashlight app in Android

Flashlight Android App :

Android Technologies provide many features to do with android devices . It gives freedom to play with hardware and software of android devices by Android Technology which uses JAVA and XML for coding and interface part. Flashlight app consist torch embedded in Android device also works with camera. Android app is just revoked the inbuilt system of android device by writing a small code in Android Studio  mentioned below.

To make Flashlight Android App, First we need Android Studio along with updated SDK so that there will be no issue occurred for building the project for Flashlight. After having Android Studio , Just follow all the steps given below . You’ll get complete code for Flashlight Android app here , Don’t  skip the steps .

1. create new project :

First create new project in Android Studio with Empty Activity.

2. PERMISSIONS :

For Flashlight App , We need to get permissions for revoking the flashlight of Android device in Android Manifest file in Android Studio. See the permissions code below:

3.  Main Activity.java


public class FlashLightActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "FlashLightActivity";
private static final String LONG_PRESS = "long_press";
public static final String WHITE = "white";
private final Flash flash = new Flash();
private View background;
private ToggleButton theButton;
private Drawable dark;
private boolean changeColor = false;

private SharedPreferences sharedPreferences;

public class FlashTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
return flash.on();
}
@Override
protected void onPostExecute(Boolean success) {
theButton.setEnabled(true);
if (! success) {
Toast.makeText(FlashLightActivity.this, "Failed to access camera.", Toast.LENGTH_SHORT).show();
}
}
}

public class WhiteTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
return sharedPreferences.getBoolean(WHITE, false);
}

@Override
protected void onPostExecute(Boolean aBoolean) {
changeColor = aBoolean;
if (changeColor && theButton.isChecked()) {
background.setBackgroundColor(Color.WHITE);
} else {
background.setBackgroundDrawable(dark);
}
}
}

public class PressTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
return sharedPreferences.getBoolean(LONG_PRESS, false);
}

@Override
protected void onPostExecute(Boolean aBoolean) {
background.setLongClickable(aBoolean);
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

theButton = (ToggleButton) findViewById(R.id.flashlightButton);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

ViewParent vp = theButton.getParent();
if (vp instanceof View) {
background = (View) vp;
background.setOnLongClickListener(new LongClickListener());
dark = background.getBackground();
} else {
Log.e(TAG, "Background isn't a view!");
background = new View(this);
}

ImageSpan imageSpan = new ImageSpan(this, R.drawable.power_symbol);
SpannableString content = new SpannableString("X");
content.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

theButton.setText(content);
theButton.setTextOn(content);
theButton.setTextOff(content);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.i(TAG, "Changed pref: " + key);
switch (key) {
case LONG_PRESS:
new PressTask().execute();
break;
case WHITE:
new WhiteTask().execute();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.settings:
intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
break;
case R.id.action_about:
intent = new Intent(this, AboutActivity.class);
startActivity(intent);
break;
}
return true;
}

@Override
public void onResume() {
super.onResume();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);

if (theButton.isChecked()) {
theButton.setEnabled(false);
new FlashTask().execute();
theButton.setKeepScreenOn(true);
} else {
flash.off();
}

new PressTask().execute();
new WhiteTask().execute();
}

@Override
public void onPause() {
super.onPause();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
flash.close();
}

public void onToggleClicked(View v) {
if (theButton.isChecked()) {
new FlashTask().execute();
v.setKeepScreenOn(true);
if (changeColor) {
background.setBackgroundColor(Color.WHITE);
}
} else {
flash.off();
v.setKeepScreenOn(false);
if (background != null) {
background.setBackgroundDrawable(dark);
}}}
public class LongClickListener implements View.OnLongClickListener {
@Override
public boolean onLongClick(View view) {
theButton.setChecked(!theButton.isChecked());
onToggleClicked(theButton);
return true;
}}}

4. Create a java class named as  “Flash.java” :

public class Flash implements Closeable {
private static final String TAG = "FLASH";

private static class CameraHolder {
public final Camera camera;
private final Camera.Parameters cameraParameters;

public CameraHolder(Camera camera) {
this.camera = camera;
this.cameraParameters = camera.getParameters();
cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}

public void on() {
camera.setParameters(cameraParameters);
camera.startPreview();
}
}

private final ExecutorService executorService = Executors.newSingleThreadExecutor();
private Future<CameraHolder> futureCamera;

public static class CameraCallable implements Callable<CameraHolder> {
private final Camera oldCamera;

public CameraCallable(Camera camera) {
oldCamera = camera;
}
@Override
public CameraHolder call() throws Exception {
if (oldCamera != null) {
oldCamera.release();
}
return new CameraHolder(Camera.open());
}
}

public synchronized void off() {
if (futureCamera == null) {
futureCamera = executorService.submit(new CameraCallable(null));
} else if (futureCamera.isDone()) {
Camera prev;
try {
prev = futureCamera.get().camera;
} catch (Exception e) {
prev = null;
}
futureCamera = executorService.submit(new CameraCallable(prev));
}
}

public synchronized boolean on() {
Log.i(TAG, "Got button press");
if (futureCamera == null) {
Log.w(TAG, "Camera is null");
futureCamera = executorService.submit(new CameraCallable(null));
}
if (!futureCamera.isDone()) {
Log.i(TAG, "Waiting for camera");
}
try {
futureCamera.get().on();
Log.i(TAG, "Light is on");
} catch (Exception e) {
Log.e(TAG, "Failed to activate flash", e);
return false;
}
return true;
}

public synchronized void close() {
if (futureCamera != null) {
try {
futureCamera.get().camera.release();
} catch (Exception e) {
Log.e(TAG, "Failed to release camera", e);
}
}
futureCamera = null;
}

}

5. Create another java class named as “SettingsActivity.java”:


public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

6.  Build and Run the project


This is all about the Flashlight Android app . If you will get any problem about the code or anything related to the project , Tell us in Comment section .

 

About the author

Generic Bytes

Hi! we are from Generic Bytes an aspiring bloggers with an obsession of things tech.This blog is dedicated to people who want to learn the technology.

View all posts

Leave a Reply

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