2

我想检索来电/去电的电话号码,但有时传递的电话号码是null. 我无法重现它,但我的客户报告说有时它不起作用。我可以确认这一点,因为我有关于此的日志(每天约 1000 次电话号码为空)。

我有 2 个不同BroadcastReceiver的扩展WakeLockBroadcast。s 在 Android的IntentService清单文件中声明。

这是拨出电话的接收方:

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);
        }
    }
}

这是来电的接收者:

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);
        }
    }
}

Android 的清单文件:

<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>

服务:

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

我可能认为意图或意图的捆绑是空的。正如您所看到的,我在 Intent 或 bundle 是否不同于 null 之前进行检查,然后将数据设置为新创建的 Intent。

4

0 回答 0