3

I want to retrieve the incoming/outgoing call's phone number, but sometimes the delivered phone number is null. I'm unable to reproduce it, but my clients are reporting that sometimes it's not working. I can confirm this because I have logs about this (~1000 times a day the phone number is empty).

I have 2 different BroadcastReceiver's which extends WakeLockBroadcast. The IntentServices are declared in Android's manifest file.

This is the outgoing call's receiver:

public class OutgoingCallReceiver extends WakeLockBroadcast {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, OutgoingCallReceiver.PhoneService.class);

        if (intent != null && intent.getExtras() != null) {
            service.setAction(intent.getAction());

            Bundle bundle = new Bundle(intent.getExtras());
            service.putExtras(bundle);
        }

        startWakefulService(context, service);
    }

    public static class PhoneService extends IntentService {

        public PhoneService() {
            super("PhoneService outgoing call");
        }

        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            try {
                if (intent != null) {
                    String action = intent.getAction();

                    if (action != null) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            if (action.equals("android.intent.action.NEW_OUTGOING_CALL")) {
                                String number = bundle.getString(Intent.EXTRA_PHONE_NUMBER, null);

                                onOutgoingCall(number);
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            catch (Error e) {
                e.printStackTrace();
            }
            finally {
                try {
                    if (intent != null)
                        completeWakefulIntent(intent);
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }

        private void onOutgoingCall(String number) {
            if (TextUtils.isEmpty(number))
                return;

            Log.d(APPNAME, "Outgoing call: " + number);
        }
    }
}

This is the incoming call's receiver:

public class IncomingCallReceiver extends WakeLockBroadcast {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, IncomingCallReceiver.PhoneService.class);

        if (intent != null && intent.getExtras() != null) {
            service.setAction(intent.getAction());

            Bundle bundle = new Bundle(intent.getExtras());
            service.putExtras(bundle);
        }

        startWakefulService(context, service);
    }

    public static class PhoneService extends IntentService {

        public PhoneService() {
            super("PhoneService incoming call");
        }

        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            try {
                if (intent != null) {
                    String action = intent.getAction();

                    if (action != null) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            // Incoming call
                            if (action.equals("android.intent.action.PHONE_STATE")) {
                                String stateStr = bundle.getString(TelephonyManager.EXTRA_STATE, "");
                                String number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER, "");
                                int state = 0;
                                if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                                    state = TelephonyManager.CALL_STATE_IDLE;
                                } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                                    state = TelephonyManager.CALL_STATE_OFFHOOK;
                                } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                                    state = TelephonyManager.CALL_STATE_RINGING;
                                }

                                if (state == TelephonyManager.CALL_STATE_IDLE) {
                                    onCallEnd();
                                }
                                else if (state == TelephonyManager.CALL_STATE_RINGING) {
                                    onIncomingCall(number);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            catch (Error e) {
                e.printStackTrace();
            }
            finally {
                try {
                    if (intent != null)
                        completeWakefulIntent(intent);
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }

        private void onCallEnd() {

        }

        private void onIncomingCall(String phoneNumber) {
            if (TextUtils.isEmpty(phoneNumber))
                return;

            Log.d("APPNAME", "Incoming call: " + phoneNumber);
        }
    }
}

The Android's manifest file:

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

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

Services:

<service android:name=".broadcast.IncomingCallReceiver$PhoneService" />
<service android:name=".broadcast.OutgoingCallReceiver$PhoneService" />

I might think that the intent or the intent's bundle is empty. As you can see, I'm making a check before if intent or bundle is different than null and after that setting the data to the newly created intent.

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137

0 Answers0