<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kiosk.torch" >
<uses-permission android:name="android.permission.CAMERA" />
<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>
Note: Since the uses-permission has been modified, please ignore the warning that camera has been deprecated in Android 5.0 or above.
2. Edit MainActivity.java as below:
package com.example.kiosk.torch;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import static android.widget.ToggleButton.OnCheckedChangeListener;
public class MainActivity extends ActionBarActivity {
ToggleButton myToggleButton;
private Camera myCamera;
Parameters myParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
{
Toast.makeText(getApplicationContext(), "Flash Detected!!", Toast.LENGTH_LONG).show();
try {
myCamera = Camera.open();
myParams = myCamera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera error: ", e.getMessage());
}
}
else
{
Toast.makeText(getApplicationContext(), "No Flash Detected!!", Toast.LENGTH_LONG).show();
}
if ((myCamera == null) || (myParams == null)) {
return;
}
myToggleButton = (ToggleButton)findViewById(R.id.toggleButton);
myToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
myParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
myParams.setFlashMode(Parameters.FLASH_MODE_OFF);
}
myCamera.setParameters(myParams);
myCamera.startPreview();
}
});
}
========================