[英]phone number from incoming call not working android
I have a PhoneStateListener
that is used to open an app once a phone call is finished. 我有一个
PhoneStateListener
,用于在通话结束后打开应用程序。 It works fine, but I also need to get the incoming calls number, and using the incomingNumber
string does not work. 它工作正常,但我还需要获取来电号码,并且使用
incomingNumber
字符串不起作用。 Here is my code: 这是我的代码:
public class PhoneStateManager extends PhoneStateListener{
public static Boolean usingPhone = false;
private MainActivity mainactivity;
public PhoneStateManager(MainActivity main){
mainactivity = main;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
if(usingPhone){
Log.d("DEBUG", "number (PSM): " + incomingNumber);
mainactivity.askQuestion(incomingNumber);
}
usingPhone = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
usingPhone = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
usingPhone = false;
break;
}
}
}
The LogCat statements print out this when a phone call takes place: 拨打电话时,LogCat语句会打印出以下内容:
Here is how I initialize the PhoneStateManger: 这是我初始化PhoneStateManger的方法:
TelephonyManager tManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateManager listener = new PhoneStateManager(this);
tManager.listen(listener, PhoneStateManager.LISTEN_CALL_STATE);
I can't figure out why the number is always null. 我不知道为什么数字总是为空。 Any ideas?
有任何想法吗?
You have to save the incomingNumber in ringing or offhook 您必须将传入号码保存为振铃或摘机
public class PhoneStateManager extends PhoneStateListener{
public static Boolean usingPhone = false;
private MainActivity mainactivity;
private String mIncomingNumber;
public PhoneStateManager(MainActivity main){
mainactivity = main;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
if(usingPhone){
Log.d("DEBUG", "number (PSM): " + mIncomingNumber);
mainactivity.askQuestion(mIncomingNumber);
}
usingPhone = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
usingPhone = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
mIncomingNumber = incomingNumber;
usingPhone = false;
break;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.