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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1853220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:43:39  来源:igfitidea点击:

Retrieve incoming call's phone number in Android

androidtelephonymanagerphone-state-listener

提问by jakob

I would like to retrieve the incoming call's phonenumber and do something with it like the do in http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-that-puts-telemarketers-on-alert/

我想检索来电的电话号码并对其进行处理,就像http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-这让电话推销员警觉/

Could you please help me because I can't find any information about this. Where do i start and how do i get hold of the phonenumber?

你能帮我吗,因为我找不到任何关于这方面的信息。我从哪里开始,如何获得电话号码?



Ok so currently my code looks like below. When I place the call the CustomBroadcastReceiver catches it and the log message is printed out. I can retrieve the telephone number from the bundle. But! I can't get hte CustomPhoneStateListener to work. As you can see I have registered my customPhoneState listener to the receiver but the log message never get's printed out from the CustomPhoneStateListener class. What am I my missing here? Is my thinking correct?

好的,目前我的代码如下所示。当我拨打电话时,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);

}

采纳答案by John Feminella

Use PhoneStateListener. It has an onCallStateChangedhandler; one of the supplied arguments you'll get is a Stringcontaining the incoming phone number.

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

回答by mikeplate

Your overridden method in CustomPhoneStateListenershould be called onCallStateChanged()(and not onCallStateChange()).

您的重写方法CustomPhoneStateListener应该被调用onCallStateChanged()(而不是onCallStateChange())。

This would have been spotted by the Java compiler if you would have had the @Overrideannotation, like you have for onReceive().

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