1
votes

I want to determine the direction of calls on my app. When I run the app on my real device, making a call of which is an outgoing, under the LogCat, the direction appears as Outgoing : (Dialed Number), of which is fine by me.

But when I get an incoming call, I still get this Outgoing : (Incoming Number)

What might be the reason for this? I tried to swap my code around and see what changes it might bring it, but still no luck.

Someone who might have experienced the same thing, or know where the problem is please.

There is my code:

   @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (state == null) {
                // outgoing call
                String number = intent.getStringExtra(intent.EXTRA_PHONE_NUMBER);
                Log.i("TAG", "OUTGOING: " + phoneNumber);
                //String direction = number;

            }
            else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))        
            {
                // incoming call
                String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.i("TAG", "INCOMING: " + phoneNumber);
                String direction = number;
            }

I tried to swap my code around where String number = intent.getStringExtra(intent.EXTRA_PHONE_NUMBER); to String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); and after my else if statement change theString number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);toString number = intent.getStringExtra(intent.EXTRA_PHONE_NUMBER);` and see what changes it might bring it, but still no luck.

@Override public int onStartCommand(Intent intent, int flags, int startId) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if (state == null) {
        // outgoing call
        String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i("TAG", "OUTGOING: " + phoneNumber);
        //String direction = number;

    }
    else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))        
    {
        // incoming call
        String number = intent.getStringExtra(intent.EXTRA_PHONE_NUMBER);
        Log.i("TAG", "INCOMING: " + phoneNumber);
        String direction = number;
    }
1
What met be the reason that makes the INCOMING call to be on the OUTGOING? When I get a call, in this case IF INCOMING on my LogCat will be INCOMING : null.King

1 Answers

0
votes

I fixed my problem, by just adding this to my PhoneReceiver class

  //my declaration
        public static String CALL_DIRECTION;
        public static String phoneNumber;

        else if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                                if (phoneNumber == null)
                                phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                                CALL_DIRECTION = "INCOMING";

    Intent myIntent = new Intent(context, RecordService.class);                 myIntent.putExtra("commandType",FixedContants.STATE_INCOMING_NUMBER);
                        myIntent.putExtra("phoneNumber",

 phoneNumber);
                    CALL_DIRECTION = "OUTGOING";

This solved my problem.