android - flashlight not working on some devices -
i made simple flashlight app not working on devices camera led on others works fine. can tell me doing wrong?
here main activity:
public class mainactivity extends activity { private camera camera; private camera.parameters parameters; private imagebutton flashlightbutton; boolean isflashlighton = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); adview madview = (adview) findviewbyid(r.id.adview); adrequest adrequest = new adrequest.builder().build(); madview.loadad(adrequest); flashlightbutton = (imagebutton)findviewbyid(r.id.flashlight_button); flashlightbutton.setonclicklistener(new flashonofflistener()); if (isflashsupported()) { camera = camera.open(); parameters = camera.getparameters(); } else { shownoflashalert(); } } private class flashonofflistener implements view.onclicklistener{ @override public void onclick(view v) { if(isflashlighton){ flashlightbutton.setimageresource(r.drawable.flashlight_off); parameters.setflashmode(parameters.flash_mode_off); camera.setparameters(parameters); camera.stoppreview(); isflashlighton = false; }else{ flashlightbutton.setimageresource(r.drawable.flashlight_on); parameters.setflashmode(parameters.flash_mode_torch); camera.setparameters(parameters); camera.startpreview(); isflashlighton = true; } } } private void shownoflashalert() { new alertdialog.builder(this) .setmessage("your device hardware not support flashlight!") .seticon(android.r.drawable.ic_dialog_alert).settitle("error") .setpositivebutton("ok", new onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); finish(); } }).show(); } private boolean isflashsupported() { packagemanager pm = getpackagemanager(); return pm.hassystemfeature(packagemanager.feature_camera_flash); } @override protected void ondestroy() { if(camera != null){ camera.stoppreview(); camera.release(); camera = null; } super.ondestroy(); } }
the problem you're having has how manufacturers have different ways putting camera led in flashlight mode , how older devices running dated versions of android used enable led. samsung , htc have issues, simplest way go around use switch-case statements turn flashlight on/off different types of devices. question has been asked several times before way go search on here different ways of turning on flashlight different manufacturers , android versions. write logic gets device's attributes, tests them using switch-case or if-else , you're supposed each vendor. @ least, that's best way ensure full compatibility.
Comments
Post a Comment