We have one of 360 mobile phone assistants, called "the Most Beautiful Flashlight",:
To turn the flash on or off with the flashlight switch onthe diagram, OK, here's the code to implement this feature:
public class Mainactivity extends Activity implements Onclicklistener {private ImageView iv_off_on;private ImageView iv_l Ight;private Parameters parameter = null;private camera camera = null; @Overrideprotected void OnCreate (Bundle Savedinstan Cestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_main); Initview (); Initlistener ();} private void Initlistener () {Iv_off_on.setonclicklistener (this);} private void Initview () {iv_off_on = (ImageView) Findviewbyid (r.id.iv_off_on); iv_light = (ImageView) Findviewbyid ( R.id.iv_light); iv_light.setvisibility (view.invisible);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.iv_off_on:if (iv_off_on.isselected ()) {//Off iv_off_ On.setselected (false); Iv_light.setvisibility (view.invisible);p Arameter.setflashmode (Parameters.flash_mode_off) ; camera.setparameters (parameter); Camera.release ();} else {//Turn on iv_off_on.setselected (true); iv_light.setvisibility (view.visible); camera = CAmera.open (); Camera.startpreview ();p arameter = Camera.getparameters ();p Arameter.setflashmode (Parameters.FLASH_ Mode_torch); camera.setparameters (parameter);} Break;default:break;}}}
Finally, add the appropriate permissions in the Androidmanifest.xml:
<uses-permission android:name= "Android.permission.FLASHLIGHT"/>
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-feature android:name= "Android.hardware.camera"/>
<uses-feature android:name= "Android.hardware.autofocus"/>
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45504303 Emotional Control _
Learn how Android works in the simplest way to turn on the Flash (35)