Android Flash Light Example
1. The Application First Step Create a New Project Start project
2. Rename Application Name Ex: Flash Light Below Screen
Click Next
3. After Spacific Minium sdk API 23 Select Lolipop 5.0
Click Next
4. Final Select Blank Activity And
Click Next
5. Then Select MainActivity Click Finish
Note: Flash Light Application is Your Android Device hardwear of Flash
suppose from Not sure support any device
1. MainActivity.Java
package com.example.flash;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageBut
public class MainActivity extends ActionBarActivity {
private ImageButton flashLight;
private Camera camera;
private Parameters parameter;
private boolean deviceHasFlash;
private boolean isFlashLightOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
flashLight = (ImageButton)findViewById(R.id.flash_light);
deviceHasFlash = getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!deviceHasFlash){
Toast.makeText(MainActivity.this, "Sorry, you device does not have any camera", Toast.LENGTH_LONG).show();
return;
}
else{
this.camera = Camera.open(0);
parameter = this.camera.getParameters();
}
flashLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isFlashLightOn){
turnOnTheFlash();
}else{
turnOffTheFlash();
}
}
});
}
private void turnOffTheFlash() {
parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
this.camera.setParameters(parameter);
this.camera.stopPreview();
isFlashLightOn = false;
flashLight.setImageResource(R.drawable.buttonoff);
}
private void turnOnTheFlash() {
if(this.camera != null){
parameter = this.camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
this.camera.setParameters(parameter);
this.camera.startPreview();
isFlashLightOn = true;
flashLight.setImageResource(R.drawable.buttonon);
}
}
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
parameter = camera.getParameters();
} catch (RuntimeException e) {
System.out.println("Error: Failed to Open: " + e.getMessage());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
super.onStop();
if(this.camera != null){
this.camera.release();
this.camera = null;
}
}
@Override
protected void onPause() {
super.onPause();
turnOffTheFlash();
}
@Override
protected void onResume() {
super.onResume();
if(deviceHasFlash){
turnOffTheFlash();
}
}
@Override
protected void onStart() {
super.onStart();
getCamera();
}
2.Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="taskreminderapp.techsect.com.techlock.MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/flash_light"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/buttonoff"
android:background="@android:color/transparent"
android:contentDescription="@null"
android:layout_marginTop="140dp"/>
</RelativeLayout>
3.Activity Mainifests.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
4.save images of drawable folder
5.String.xml
<resources> <string name="app_name">Spaceo FlashLight</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> </resources>
ReplyDeleteDon’t purchase an flash Light or disco light and don’t waste your money install this app in your android device and solve your disco with music, flashlight & strobe light problem.