![Control Flash Light Control Flash Light](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEZDII2GcANOHe3-1MHVdLneOrYd33puJ-ZtflphnR8DYgvmSkozAk7cj_wEwWX9tMcaAlbbk_T6TPWys6hgU4rCDewTjyef8rwIb2CFr42-EZCTwbcC-WrsOqol8ZvFiZGUbq5VBeBSye/s400/AndroidFlashLight_01.png)
Modify main.xml to have a button to toggle torch on/off.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/flashcontrol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
main code
package com.exercise.AndroidFlashLight;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidFlashLight extends Activity {
Camera camera = null;
Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button FlashLightControl = (Button)findViewById(R.id.flashcontrol);
FlashLightControl.setText("Set FLASH_MODE_TORCH");
FlashLightControl.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(camera == null){
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
FlashLightControl.setText("Set FLASH_MODE_OFF");
}else{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
FlashLightControl.setText("Set FLASH_MODE_TORCH");
}
}});
}
}
Modify AndroidManifest.xml to grant permission of "android.permission.CAMERA".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidFlashLight"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidFlashLight"
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>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8p8OYSJ34x9VZ9cj4IzAOVtRlBOzcLrPRJWQ4iuiJI1eCKjrLYOEL2p2RYTNBidfvJuDOpoiZjXICYCF5ifVUBp09IMGbHsHFSOko1_Xdf4gYrG9MbcBSZDywv5SWAQhCav5OT9OGPE5T/s200/icons_download.gif)
6 comments:
Don´t Work on my Xperia X10...
hello Francisco Nuñez Hernandez,
Yes, it doesn't work on Xperia X10! Sorry about that.
Xperia have difference control, I tried to make it work on Xperia but cannot find any solution...
thanks for the reply.
Excellent blog, I love it.
Greetings.
how to check the flashlight on emulator?
hello saikiran,
I think emulator have no flashlight, so cannot check on emulator.
nice post! thanks alot.
Post a Comment