如何获取正在拨打的号码,即

时间:2016-08-08 09:14:15

标签: java android eclipse android-studio telephony

我在传出号码上一直得到一个null值我从网上尝试了几个解决方案。

在我的清单中,我有这些相关的行

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

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" > </uses-permission>
  <receiver android:name=".home.OutgoingReceiver"  android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.PHONE_STATE" >
                   <action android:name="android.intent.action.NEW_OUTGOING_CALL" />


               </action>
           </intent-filter>
       </receiver>

我的接收器看起来很喜欢这个,你会注意到我一直试图以各种方式检索这个号码,但我一直都是空值

public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {


            final String originalNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
            Log.d("APP", "outgoing,ringing:" + originalNumber);
        }
        String ph = intent.getExtras().getString(OutgoingReceiver.INTENT_PHONE_NUMBER);
        // savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        String phoneNumber = getResultData();
        if (phoneNumber == null) {
            // We could not find any previous data. Use the original phone number in this case.
            phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        }
        String phoneNumberTest = intent.getExtras().getString(OutgoingReceiver.INTENT_PHONE_NUMBER);

1 个答案:

答案 0 :(得分:0)

首先将此添加到您的清单中:

<action android:name="android.intent.action.NEW_OUTGOING_CALL" />

然后将此添加到您的接收器:

if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
          String outoingNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
            return;
        }