在Android中检索来电的电话号码

时间:2009-12-05 19:42:25

标签: android telephonymanager phone-state-listener

我想检索来电的电话号码,并像做的那样对它做点什么 在http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-that-puts-telemarketers-on-alert/

你能不能帮助我,因为我找不到任何有关此事的信息。 我从哪里开始,如何掌握电话号码?


好的,目前我的代码如下所示。当我拨打电话时,CustomBroadcastReceiver捕获它并打印出日志消息。我可以从捆绑中检索电话号码。但!我不能让他的CustomPhoneStateListener工作。正如您所看到的,我已将customPhoneState侦听器注册到接收器,但日志消息永远不会从CustomPhoneStateListener类中打印出来。我在这里失踪了什么? 我的想法是否正确?


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

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

public void onCallStateChange(int state, String incomingNumber){

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    Log.v(TAG, incomingNumber);

    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(TAG, "RINGING");
            break;
    }   
}

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


    Bundle bundle = intent.getExtras();
    String phoneNr= bundle.getString("incoming_number");
    Log.v(TAG, "phoneNr: "+phoneNr);

}

3 个答案:

答案 0 :(得分:25)

使用PhoneStateListener。它有一个onCallStateChanged处理程序;您将获得的一个参数是String,其中包含传入的电话号码。

答案 1 :(得分:5)

CustomPhoneStateListener中被覆盖的方法应该被称为onCallStateChanged()(而不是onCallStateChange())。

如果您拥有@Override注释,那么Java编译器就会发现这一点,就像您对onReceive()一样。

答案 2 :(得分:1)

以上答案现在已经过时了。适用于 Android 7 及更低版本。

对于 android 9 及更高版本,您必须在具有权限的 Androidmanifest.xml 中添加另一个权限

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

没有电话号码将为空。对于 Android 8,我不确定。

PhoneStateReciever.java

    package com.incomingcalls;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.Toast;


    public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            System.out.println("Receiver start");
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.e("Incoming Number", "Number is ," + incomingNumber);
            Log.e("State", "State is ," + state);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
                Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();


            }
            if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
                Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
            }
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
}

AnroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.incomingcalls">
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".PhoneStateReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>