Saturday, 25 February 2017

Flashlight Android App | Source Code for Android FlashLight Application Run in Android Studio

Source code for Flashlight Android App is given below. 




package com.example.admin.flashlightapp;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    //variable declaration
    ImageButton imagebuttonoff;
    public static Camera cmr;
    Camera.Parameters parameters;
    boolean isFlash = false;
    boolean isOn = false;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //variable casting and assigning condition        
imagebuttonoff = (ImageButton) findViewById(R.id.imagebuttonoff);
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))

        {

            cmr = Camera.open();
            parameters = cmr.getParameters();
            isFlash = true;

        }


        //setting onclicklistner on off image ie on black imagebutton..        
imagebuttonoff.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                if (isFlash)
                // if your mobile support flash light
                {


                    //if flash light is not on                    
if (!isOn) {

                        imagebuttonoff.setImageResource(R.drawable.on); 
//replace offswitch image with onswitch image.                        
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
//also set on flash torch parameter..                        
cmr.setParameters(parameters);//by using object  cmr.                        
cmr.startPreview();
                        isOn = true;// now flashlight is on..ie true

                    } else {
         //exactly opposite as above ie as switch on..ie how to off flash light..                       
 imagebuttonoff.setImageResource(R.drawable.off);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
 cmr.setParameters(parameters);
                        cmr.stopPreview();
                        isOn = false; // now flash light is off..
                    }
                } else { // if your mobile does not support flash light..
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error"); 
// this will set title for error message...                   
 builder.setMessage("Flash light not available"); 
// will display message                    
builder.setPositiveButton("ok", new DialogInterface.OnClickListener()
 {//so that you can click on "ok"..                       
 @Override                     
   public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss(); // so application is dismiss                            
finish();
                        }
                    });


 AlertDialog alertDialog = builder.create(); 
//this will shows dialog box..
/which is showing above error message..                    
alertDialog.show();

                }

            }
        });


    }

    // calls to destroy    
@Override    
protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // on pause turn off the flash        
cmr.stopPreview();

    }

    @Override    
protected void onRestart() {
        super.onRestart();

    }


    @Override    
protected void onResume() {
        super.onResume();
        // on resume turn on the flash        
if (isOn)
            cmr.startPreview();

    }

    @Override    
protected void onStart() {
        super.onStart();
        // on starting the app get the camera params        
cmr.startPreview();

    }


    @Override   
 protected void onStop() { // this will stop out flash light..       
 super.onStop();

    }


}
Changes in Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    
package="com.example.admin.flashlightapp">
    <uses-permission android:name="android.permission.CAMERA">
</uses-permission>
    <uses-feature android:name="android.hardware.camera2">
</uses-feature>

    <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" 
android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




Important Tips - 

<activity android:name=".MainActivity" 
android:configChanges="orientation|screenSize">

This line is use so that flashlight should active on both potrait mode as well as
landscape mode..




No comments:

Post a comment