android - Blink flashlight when an incoming call receives -
i trying app makes mobile led flashlight blink when incoming call received.
broadcastreceiver code:
public class incomingcallinterceptor extends broadcastreceiver { // 1 private camera camera; private boolean isflashon; private boolean hasflash; camera.parameters params; @override public void onreceive(context context, intent intent) { // 2 string state = intent.getstringextra(telephonymanager.extra_state); // 3 string msg = "ringing" + state; if (telephonymanager.extra_state_ringing.equals(state)) { // 4 string incomingnumber = intent.getstringextra(telephonymanager.extra_incoming_number); // 5 // msg += ". incoming number " + incomingnumber; // todo place "do when phone rings" ;-) string mystring = "0101010101"; long blinkdelay = 50; getcamera(); (int = 0; < mystring.length(); i++) { if (mystring.charat(i) == '0') { // params.setflashmode(camera.parameters.flash_mode_on); turnonflash(); } else { // params.setflashmode(camera.parameters.flash_mode_off); turnoffflash(); } try { thread.sleep(blinkdelay); } catch (interruptedexception e) { e.printstacktrace(); } } } toast.maketext(context, msg, toast.length_long).show(); } private void turnonflash() { if (!isflashon) { if (camera == null || params == null) { return; } params = camera.getparameters(); params.setflashmode(camera.parameters.flash_mode_torch); camera.setparameters(params); camera.startpreview(); isflashon = true; } } private void getcamera() { if (camera == null) { try { camera = camera.open(); params = camera.getparameters(); } catch (runtimeexception e) { log.e("camera error. failed open. error: ", e.getmessage()); } } } private void turnoffflash() { if (isflashon) { if (camera == null || params == null) { return; } params = camera.getparameters(); params.setflashmode(camera.parameters.flash_mode_off); camera.setparameters(params); camera.stoppreview(); isflashon = false; } } }
this how trying that. working fine first call. when make second call light not blinking getting toast message properly. doing wrong here??
just simple, call:
camera.release();
after
camera.stoppreview();
Comments
Post a Comment