Not getting correct value when retrieving current outgoing phone number in android
Asked Answered
F

1

2

I am using the following broadcast receiver to retrieve outgoing phone number.

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int events = PhoneStateListener.LISTEN_CALL_STATE;
    tm.listen(phoneStateListener, events);
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {



        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:

            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:


                dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Toast.makeText(mContext, "Local Call Number- " + dialingNumber,Toast.LENGTH_SHORT).show();


                break;
            }
                return;
        }



        Log.i(">>>Broadcast", "onCallStateChanged " + callState);
        super.onCallStateChanged(state, incomingNumber);
    }
};

Sometimes It returns null or number I called before.Is there another method to retrieve incoming/outgoing call phone number of the current call?

Fossil answered 18/3, 2013 at 9:34 Comment(0)
H
13

for outgoing calls, use this:

manifest:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

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

code:

...
@Override
public void onReceive(final Context context, final Intent intent) {
    Log.d("APP", "ACTION:" + intent.getAction());

    if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
        final String originalNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.d("APP", "outgoing,ringing:" + originalNumber);
    }
    ...

for incoming calls, use this:

manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

code:

@Override
public void onReceive(final Context context, final Intent intent) {
    Log.d("APP", "ACTION:" + intent.getAction());
    final String stringExtra = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(stringExtra)) {
            final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.d("APP", "incoming,ringing:" + incomingNumber);
        } else if (stringExtra.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.d("APP", "hanged" + incomingNumber );
        } else if (stringExtra.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            final String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.d("APP", "answered" + incomingNumber );
        }
    }
Horsepowerhour answered 25/4, 2013 at 15:40 Comment(6)
How can we know that call is ended?Dostie
@MdMohsin For both incoming and outgoing calls, you can see that there is a text I've written "hanged" .Horsepowerhour
Okk sir..One more question if you don't mind. I am using outgoing and incoming call receiver but it sometime fire and sometime not..even i used priority in intent filer in androidmanifast.xmlDostie
check this please #27417635Dostie
@MdMohsin It's possible that the intent is being sent a bit later. Have you waited long enough?Horsepowerhour
Please check my question posted on stack overflow..my code is working correctly but giving error for outgoing call..even given outgoing number correctly.Dostie

© 2022 - 2024 — McMap. All rights reserved.