如何从来电中获取电话号码?

时间:2012-10-31 09:06:28

标签: android broadcastreceiver telephony

如果Android中有来电,我该如何获取电话号码?

7 个答案:

答案 0 :(得分:30)

Broadcast receiverServiceReceiver在Manifest中分配其操作。

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

PhoneStateListener添加到您的TelephonyManager,PhoneStateListener,并使用Incoming number参数覆盖onCallStateChanged()。多数民众赞成。

<强> ServiceReceiver.Java

public class ServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                System.out.println("incomingNumber : "+incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
}

答案 1 :(得分:4)

以下解决方案帮助我检索传入和传出的电话号码。

清单中包含的内容

1)许可:

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

2)广播接收器:

<receiver android:name=".AnswerCallBroadcastReceiver">
        <intent-filter> 
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
</receiver>

要包含在BroadcastReceiver类中的内容

    public class AnswerCallBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {

            if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){

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

                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    Log.d(TAG, "Inside Extra state off hook");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_PHONE_NUMBER);
                    Log.e(TAG, "outgoing number : " + number);              
                }       

                else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){   
                    Log.e(TAG, "Inside EXTRA_STATE_RINGING");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    Log.e(TAG, "incoming number : " + number);
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                    Log.d(TAG, "Inside EXTRA_STATE_IDLE");
                }   
            }   
        }
    }

答案 2 :(得分:2)

这肯定对你有帮助。

这是一个实现,它允许您检索电话号码(如果它是来电号码,以及呼叫结束时) - 请注意处理程序()代码。

private class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

@Override
public void onCallStateChanged(int state, String incomingNumber) {

    if (TelephonyManager.CALL_STATE_RINGING == state) {
        // phone ringing
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }

    if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
        // active
        Log.i(LOG_TAG, "OFFHOOK");

        isPhoneCalling = true;
    }

    if (TelephonyManager.CALL_STATE_IDLE == state) {
        // run when class initial and phone call ended, need detect flag
        // from CALL_STATE_OFFHOOK
        Log.i(LOG_TAG, "IDLE number");

        if (isPhoneCalling) {

            Handler handler = new Handler();

            //Put in delay because call log is not updated immediately when state changed
            // The dialler takes a little bit of time to write to it 500ms seems to be enough
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // get start of cursor
                      Log.i("CallLogDetailsActivity", "Getting Log activity...");
                        String[] projection = new String[]{Calls.NUMBER};
                        Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
                        cur.moveToFirst();
                        String lastCallnumber = cur.getString(0);
                }
            },500);

            isPhoneCalling = false;
        }

    }
} 

然后在onCreate或onStartCommand代码中添加并初始化侦听器:

  PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
        .getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
        PhoneStateListener.LISTEN_CALL_STATE);

答案 3 :(得分:1)

答案 4 :(得分:1)

您可以找到解决方案here

Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");

答案 5 :(得分:1)

通过使用接受的答案,您将在呼叫状态的每次更改时注册一个新的侦听器。它将导致对同一事件进行多次onCallStateChanged调用。

答案 6 :(得分:1)

以上答案是正确的,但请不要忘记权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

注意,从Android 9格式开始,您需要阅读此电话号码才能申请此许可
<uses-permission android:name="android.permission.READ_CALL_LOG" />