Like Us on Facebook

How to make FlashLight Application in Android


Hello everyone , as  per  specific demand  I am going to share How to Make FlashLight Application, it give you clear  concept of making FlahLight Application . FlashLight Application helps to turn on  light  in dark.



Prerequisites:
 
  • JDK 6.0 or above
  • Android Studio


Steps to follow:  
 
  1. Create a New Android Studio project name FlashLight.
  2. Select minimum  API level 14 so it will run Maximum of Android device that are Active on google Play then click next.
  3. Select Blank Activity and your Activity name is MainActivity click next > click finish.
  4. Inside MainActivity Class write the following code;

 
public class MainActivity extends Activity {
//flag to detect flash is on or off
private boolean isLightOn = false;
private Camera camera;
private Button btn;

@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
btn.setOnClickListener(new OnClickListener() {
     
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isLightOn){
Log.i("info", "Flashlight is turn off") ;
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLightOn = false;

}else{
Log.i("info","torch is turn on");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLightOn = true;
}
}
});
}
}



5. And main.xml will be like below
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relative"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Press"
/>
</RelativeLayout>



6. And AndroidManifest.xml of the Application will be like below
 
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.tutorial.flash"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />

<permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" /> -->

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
       
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also