在android中检索当前的外出电话号码时没有得到正确的值

时间:2013-03-18 09:34:25

标签: android phone-state-listener

我正在使用以下广播接收器来检索拨出的电话号码。

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

有时它返回null或我之前调用的数字。是否有另一种方法来检索当前呼叫的来电/去电电话号码?

1 个答案:

答案 0 :(得分:13)

拨出电话,请使用:

清单:

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

代码:

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

来电,请使用:

清单:

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

代码:

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