Copy Code code as follows:
public class Handlightactivity extends activity implements onclicklistener{
Private ToggleButton ToggleButton;
Private Camera M_camera;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.handlight);
ToggleButton = (ToggleButton) This.findviewbyid (R.id.togglebutton1);
Togglebutton.setonclicklistener (this);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void OnClick (View v) {
ToggleButton TB = (ToggleButton) v;
if (!tb.ischecked ()) {
Packagemanager pm= This.getpackagemanager ();
Featureinfo[] Features=pm.getsystemavailablefeatures ();
for (Featureinfo f:features)
{
if (PackageManager.FEATURE_CAMERA_FLASH.equals (f.name))//Determine if the device supports Flash
{
if (null = = M_camera)
{
M_camera = Camera.open ();
}
Camera.parameters Parameters = M_camera.getparameters ();
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);
M_camera.setparameters (parameters);
M_camera.startpreview ();
Togglebutton.setbackgroundcolor (0X30FFFFFF);
}
}
}else{
if (M_camera!= null)
{
M_camera.stoppreview ();
M_camera.release ();
M_camera = null;
}
Togglebutton.setbackgroundcolor (0xFFFFFFFF);
}
}
}
Handlight.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<togglebutton android:id= "@+id/togglebutton1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:checked= "true"
android:text= "ToggleButton"/>
</LinearLayout>
need to add permissions
<uses-permission android:name= "Android.permission.FLASHLIGHT"/>
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-permission android:name= "Android.hardware.camera"/>
Note: Not all devices are supported
On the internet there is another way to call the system to hide the API, but I tried not to respond, may be the system version problem, back up
Direct control with Ihardwareservice
Android1.5 used to provide this interface directly, after that, we need to do it ourselves.
Create a new package Android.os in your project, and create a new Ihardwareservice.aidl file that reads:
Copy Code code as follows:
Package Android.os;
/** {@hide} * *
Interface Ihardwareservice
{
Obsolete Flashlight Support
Boolean getflashlightenabled ();
void Setflashlightenabled (Boolean on);
}
and then introduce it into your program.
Copy Code code as follows:
Import Android.os.IHardwareService;
/**
* Set up and close the flash
* @param isenable
* @author Linc
* @date 2012-3-18
*/
private void Setflashlightenabled (Boolean isenable)
{
Try
{
Method method = Class.forName ("Android.os.ServiceManager"). GetMethod ("GetService", String.class);
IBinder binder = (ibinder) method.invoke (null, new object[] {"Hardware"});
Ihardwareservice Localhardwareservice = IHardwareService.Stub.asInterface (binder);
Localhardwareservice.setflashlightenabled (isenable);
}
catch (Exception e)
{
E.printstacktrace ();
}
}
so you can turn on the flash. Permissions to use:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.FLASHLIGHT"/>
<uses-permission android:name= "Android.permission.HARDWARE_TEST"/>