The above code just give the permission for the flashLight.
You have to write a code to turnon and off the Flash
I am adding the snippet of the demo i did for turning on and offing the flash light.
MainActivity.java
package com.example.balaji.myapplication;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Camera cam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button turnOn = (Button)findViewById(R.id.turnOnFlashLight);
turnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turnOnFlashlight();
}
});
Button turnOff = (Button)findViewById(R.id.turnOffFlashLight);
turnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turnOffFlashlight();
}
});
}
@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);
}
public void turnOnFlashlight(){
cam = Camera.open();
Camera.Parameters p = cam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
public void turnOffFlashlight(){
cam.release();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity">
<Button
android:id="@+id/turnOnFlashLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
/>
<Button
android:id="@+id/turnOffFlashLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.balaji.myapplication" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
I am also giving the stackoverflow links for more information on this.
How to turn on FlashLight in Lollipop programmatically Android