Android Studio Flashlight App Basic
Android Studio Flashlight App Basic
I.
II.
Flashlight Code
MainActivity.java code:
package com.example.philipgo.flashlightapp;
//Dont forget about these import statements. They enable you to refer to
the different classes available in Android
import
import
import
import
import
import
android.content.Context;
android.graphics.PixelFormat;
android.os.SystemClock;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.app.Activity;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.app.AlertDialog;
android.content.DialogInterface;
android.content.pm.PackageManager;
android.hardware.Camera;
android.hardware.Camera.Parameters;
android.os.Bundle;
android.util.Log;
android.view.SurfaceHolder;
android.view.SurfaceView;
android.view.View;
android.widget.Button;
android.view.SurfaceHolder.Callback;
android.widget.TextView;
java.io.IOException;
//the code below sets what happens when the user presses the turn on button
flash_on.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
turnOnFlash();
}
});
//the code below sets what happens when the user presses the turn off button
flash_off.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
turnOffFlash();
}
});
}
//turnOnFlash() contains the code needed to turn the phones flashlight on
private void turnOnFlash()
{
if(!isFlashOn)
{
if(camera == null || params == null)
return;
try {
camera.setPreviewDisplay(mholder);
} catch (IOException e) {
e.printStackTrace();
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
//turnOffFlash() contains the code needed to turn the phones flashlight off
private void turnOffFlash()
{
if(isFlashOn)
{
if(camera == null || params == null)
return;
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
@Override
protected void onPause()
{
super.onPause();
turnOffFlash();
//turns off the flashlight when the application is paused
}
@Override
protected void onRestart()
{
super.onRestart();
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onStart()
{
super.onStart();
getCamera();
}
@Override
protected void onStop()
{
super.onStop();
if(camera != null)
{
camera.release();
camera = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mholder = holder;
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
holder = null;
}
}
AndroidManifest.xml Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.philipgo.flashlightapp">
//The permission statements below enable you to use the phones various
hardware, which in this case is the camera
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<uses-feature android:name="android.hardware.camera.flash"
android:required="false" />
<uses-permission android:name="android.hardware.camera.flash"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:screenOrientation="portrait"> //disables landscape mode
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
III.
MainActivity.Java code:
package com.example.philipgo.flashlightapp;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.content.Context;
android.graphics.PixelFormat;
android.os.SystemClock;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.app.Activity;
android.app.AlertDialog;
android.content.DialogInterface;
android.content.pm.PackageManager;
android.hardware.Camera;
android.hardware.Camera.Parameters;
android.os.Bundle;
android.util.Log;
android.view.SurfaceHolder;
android.view.SurfaceView;
android.view.View;
android.widget.Button;
import
import
import
import
import
import
import
android.view.SurfaceHolder.Callback;
android.hardware.SensorEventListener;
android.hardware.Sensor;
android.hardware.SensorEvent;
android.hardware.SensorManager;
android.widget.TextView;
java.io.IOException;
if(camera == null)
{
try
{
camera = Camera.open();
params = camera.getParameters();
}
catch(RuntimeException e)
{
Log.e("Camera Error.: ", e.getMessage());
}
}
}
private void turnOnFlash()
{
if(!isFlashOn)
{
if(camera == null || params == null)
return;
try {
camera.setPreviewDisplay(mholder);
} catch (IOException e) {
e.printStackTrace();
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
status.setText("ON");
}
}
private void turnOffFlash()
{
if(isFlashOn)
{
if(camera == null || params == null)
return;
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
status.setText("OFF");
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
@Override
protected void onPause()
{
super.onPause();
10
senSensorManager.unregisterListener(this);
//unregisters the sensor when the application is paused. This saves battery.
turnOffFlash();
}
@Override
protected void onRestart()
{
super.onRestart();
}
@Override
protected void onResume()
{
super.onResume();
senSensorManager.registerListener(this, senAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
//registers the sensor again upon resuming the application
}
@Override
protected void onStart()
{
super.onStart();
getCamera();
}
@Override
protected void onStop()
{
super.onStop();
if(camera != null)
{
camera.release();
camera = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mholder = holder;
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
holder = null;
11
}
}
AndroidManifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.philipgo.flashlightapp">
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<uses-feature android:name="android.hardware.camera.flash"
android:required="false" />
<uses-permission android:name="android.hardware.camera.flash"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
12