android - Why the service is not getting stop? -


hi blinking flashlight on incoming call , able using code problem flashlight not getting stopped when call attended or cut trying stop service every way flashlight doesnot stops untill loop completed in receiver class...

intent in = new intent(context, run.class);             if (state.equals(telephonymanager.extra_state_ringing)) {                 in.putextra("state", state);                 context.startservice(in);             } else if (state.equals(telephonymanager.extra_state_idle)) {                 // toast.maketext(context, "idle", toast.length_long).show();                 context.stopservice(in);              } else if (state.equals(telephonymanager.extra_state_offhook)) {                 // toast.maketext(context, "offhook", toast.length_long).show();                  context.stopservice(in);             } 

in service class...

camera cam;     parameters p;     string state;     string tag="runserve"; @override     protected void onhandleintent(intent intent) {         // todo auto-generated method stub         state = intent.getstringextra(telephonymanager.extra_state);         if(state.equals(telephonymanager.extra_state_ringing)) {         try {               cam = camera.open();             p = cam.getparameters();              string mystring = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";             long blinkdelay = 50;               (int = 0; < mystring.length(); i++) { state=intent.getstringextra(telephonymanager.extra_state);                  if (state.equals(telephonymanager.extra_state_idle)){                      break;                                        }else if (state.equals(telephonymanager.extra_state_offhook)){                     break;                       }                                 if (mystring.charat(i) == '0') {                     p.setflashmode(parameters.flash_mode_torch);                     cam.setparameters(p);                 } else {                     p.setflashmode(parameters.flash_mode_off);                     cam.setparameters(p);                 }                      thread.sleep(blinkdelay);              }         }catch (exception e) {             // todo: handle exception             log.d(tag, "in catch1");             log.d(tag, e.tostring());      } }else if (state.equals(telephonymanager.extra_state_idle)){     try {         p.setflashmode(parameters.flash_mode_off);         cam.release();     } catch (exception e) {         // todo: handle exception         log.d(tag, e.tostring());     }      stopself();     }else if (state.equals(telephonymanager.extra_state_offhook)){         try {              p.setflashmode(parameters.flash_mode_off);             cam.release();         } catch (exception e) {             // todo: handle exception             log.d(tag, e.tostring());         }          stopself();     } } 

i making app in can attend call through proximity sensor.this code has 2 intents first i.e working versions less android 4.0 , headsetunpluggedintent i.e working on android 4.1. there single way through calls can attended versions

public void onsensorchanged(sensorevent event) {         // todo auto-generated method stub         telephonymanager mgr = (telephonymanager) getsystemservice(context.telephony_service);         int callstate = mgr.getcallstate();           if(callstate==telephonymanager.call_state_ringing) {              callstate = mgr.getcallstate();             string x="0.0";             string y=(string.valueof(event.values[0]));                 if( x.equals(y)){                 //toast.maketext(getapplicationcontext(), "proxy", toast.length_short).show();                 try{                      intent = new intent(intent.action_media_button);                      i.putextra(intent.extra_key_event,new keyevent(keyevent.action_up,keyevent.keycode_headsethook));                     sendorderedbroadcast(i, "android.permission.call_privileged");                      intent headsetunpluggedintent = new intent(intent.action_headset_plug);                     headsetunpluggedintent.addflags(intent.flag_receiver_registered_only);                     headsetunpluggedintent.putextra("state", 0);                     headsetunpluggedintent.putextra("name", "headset");                     sendorderedbroadcast(headsetunpluggedintent, null);                     if(callstate==telephonymanager.call_state_offhook){                         headsetunpluggedintent=null;                         i=null;                     }else if(callstate==telephonymanager.call_state_idle){                         headsetunpluggedintent=null;                         i=null;                     }                  }catch(exception e){                     log.d(tag, e.tostring());                 }                 }                

several points here :

  • in loop, it's useless check value of state have checked in if condition. state string (i.e.immutable), it's value not going change.
  • you need get, each iteration, current state of call, using telephonymanager#getcallstate(). (not tested) :

    telephonymanager mgr = getsystemservice(context.telephony_service); int callstate = mgr.getcallstate(); if (callstate != telephonymanager.call_state_ringing )     break; 
  • you should not attempt stop intentservice, stops when work done (that when onhandleintent returns). also, intentservice javadoc states should not call stopself().


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -