简体   繁体   中英

Android - In the event of outgoing calls, are being throws automatically the state of OFF_HOOK

In the event of outgoing calls, are being throws automatically the state of OFF HOOK Hi guys, firstly my apologies for my english.

Well, in my application I'm monitoring every call, before and after. But just after of the call start, so I don't matter with "ringing" state, however my application is triggering a fake "off hook" state. When I make a call (outgoing call), my app is setting the state off hook immediately following the ringing. With this, I'm monitoring a call that I don't should, because that call can do not be answered.

Did someone already had this problem?

AndroidManifest.xml

    <receiver
        android:name=".CallStateBroadcastReceiver">
        <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

    <receiver
        android:name=".OutgoingCallBroadcastReceiver">
        <intent-filter>
          <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

CallStateBroadcastReceiver:

    CallStatePhoneStateListener phoneListener = new CallStatePhoneStateListener(context, intent);
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(
            Context.TELEPHONY_SERVICE);

        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

CallStatePhoneStateListener:

@Override
public void onCallStateChanged(int state, String incomingNumber) {

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:

            // do something
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:

            // do something
            break;

        case TelephonyManager.CALL_STATE_RINGING:

            // do something
            break;
}

OutgoingCallBroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    if (bundle == null)
        return;

    String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    // just save the number
}

Take a Static String eg:- prev_state and store the state of phone in that String at each case example if u want to know how the outgoing call is disconnected then should do something like this,

   if(state==TelephonyManager.CALL_STATE_IDLE)
{ Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
                      {
                   if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK))
                             {  
                        prev_state=state;  
                       // do something when the Call  is ended 
                              }
                       }
}

hope it helps :)

i hope this is work for u.

public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    Log.d("main", "incoming call receiver.");
    PowerManager pm = (PowerManager) c
            .getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        Log.v("idle state", "CALL_STATE_IDLE");
        // CALL_STATE_IDLE;


        if (ring == true && callReceived == false && CheckMissCall.isReject==true) {

            Log.v("missed call", "Missed call from : " + incomingNumber);
            if(CheckMissCall.isShown)
            {
                c.stopService(new Intent(c, Unlock_hud.class));

            }

            flag = true;
            if (prefs.getBoolean("main_state", true) )
            {
                Intent inter = new Intent(c, MissCall.class);
                inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                inter.putExtra("CellNumber", incomingNumber);
                c.startActivity(inter);
            }

        }


        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        // CALL_STATE_OFFHOOK;
        callReceived = true;
        Log.v("call received", "CALL_STATE_OFFHOOK  " + incomingNumber);        

        break;
    case TelephonyManager.CALL_STATE_RINGING:
        ring = true;
        // CALL_STATE_RINGING
        Log.v("call ringing", "CALL_STATE_RINGING  " + incomingNumber);
        Log.d("flags", "flags: " + flag);
        if (flag) {

            //cut = true;
            //flag = false;
            CheckMissCall call = new CheckMissCall(c);
            call.setName(incomingNumber);
            call.setNumber4Sms(incomingNumber);
            call.setContactPhoto();

            Log.d("main", "state ringing");
            prefs = PreferenceManager.getDefaultSharedPreferences(c);

            if (!prefs.getBoolean("main_state", false)) {

                return;
            }       

            if (CheckMissCall.isRunning) {

                return;
            }
            else {
                Log.d("main", "EEEEEEEEEEEEEEEE:  Unlock hud");
                Intent in = new Intent(c, Unlock_hud.class);
                in.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
                c.startService(in);

                // c.stopService(new Intent(c, Unlock_hud.class));
            }
        }
        break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM