gpt4 book ai didi

java - android 来电时的电话号码不起作用

转载 作者:行者123 更新时间:2023-12-01 13:16:24 26 4
gpt4 key购买 nike

我有一个 PhoneStateListener,用于在电话通话结束后打开应用程序。它工作正常,但我还需要获取来电号码,并且使用 incomingNumber 字符串不起作用。这是我的代码:

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;
}
}
}

当有电话调用时,LogCat 语句会打印出以下内容:

enter image description here

以下是我初始化 PhoneStateManger 的方法:

TelephonyManager tManager = (TelephonyManager) 
getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateManager listener = new PhoneStateManager(this);
tManager.listen(listener, PhoneStateManager.LISTEN_CALL_STATE);

我不明白为什么该数字总是为空。有什么想法吗?

最佳答案

您必须保存振铃或摘机时的来电号码

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;
}
}

}

关于java - android 来电时的电话号码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com