Question

Hi i am blinking the flashlight on incoming call and i am able to do that using this code but the problem is the flashlight is not getting stopped when the call is attended or cut I am trying to stop the service from every way but the flashlight doesnot stops untill the loop is 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;


            for (int i = 0; i < 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 am making the app in which i can attend the call through proximity sensor.This code has 2 intents First is i i.e working for versions less than Android 4.0 and another is headSetUnPluggedintent i.e working on Android 4.1. Is there any single way through which calls can be attended by all 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 i = 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());
                }
                }               
Was it helpful?

Solution

Several points here :

  • in the for loop, it's useless to check the value of state as you have already checked it in your if condition. state is a String (i.e.immutable), it's value is not going to change.
  • you need to get, for each iteration, the current state of the call, using TelephonyManager#getCallState(). Something like this (not tested) :

    TelephonyManager mgr = getSystemService(Context.TELEPHONY_SERVICE);
    int callState = mgr.getCallState();
    if (callState != TelephonyManager.CALL_STATE_RINGING )
        break;
    
  • You should normally not attempt to stop a IntentService, it stops itself when all work is done (that is when onHandleIntent returns). Also, the IntentService javadoc states that you should not call stopSelf().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top