在 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
Retrieve incoming call's phone number in Android
提问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 onCallStateChanged
handler; one of the supplied arguments you'll get is a String
containing the incoming phone number.
使用PhoneStateListener
. 它有一个onCallStateChanged
处理程序;您将获得的提供的参数之一是String
包含传入电话号码的参数。
回答by mikeplate
Your overridden method in CustomPhoneStateListener
should be called onCallStateChanged()
(and not onCallStateChange()
).
您的重写方法CustomPhoneStateListener
应该被调用onCallStateChanged()
(而不是onCallStateChange()
)。
This would have been spotted by the Java compiler if you would have had the @Override
annotation, like you have for onReceive()
.
如果您有@Override
注释,Java 编译器就会发现这一点,就像您对onReceive()
.