gpt4 book ai didi

android - 无法注销电话状态监听器

转载 作者:行者123 更新时间:2023-11-29 00:37:30 26 4
gpt4 key购买 nike

我正在尝试将以下内容集成到我的代码中以注销 phonestatelistener

TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}

我有一项在后台运行的服务,可以检查某个号码是否在阻止列表中。如果是,则调用 PhonestateListener

TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(new phoneStateListener(NumberInbound,Msg_to_display,mTM), PhoneStateListener.LISTEN_CALL_STATE);

但是当我从阻止列表中删除一个号码时,我想取消注册 phonestatelistner。取而代之的是 phonestatelistener 总是注册的,而我之前删除的 msisdn 仍然被阻止!

你能帮忙吗?

phonestatelistner 的代码

class phoneStateListener extends PhoneStateListener {

String inbounda;
int MessagetoDisplay;
String msg;
String reply;
TelephonyManager myt;
MyService calltoService;
boolean mBound = false;
private static final String TAG = "PHONELISTENER";

phoneStateListener(String inbound,int Message,TelephonyManager t) {
inbounda = inbound;
MessagetoDisplay = Message;
myt = t;
}

public void onCallStateChanged(int state, String incomingNumber) {

if(!incomingNumber.equals(inbounda)){

} else {

final String TAG = "Phone call";
ITelephony telephonyService;
//new code
Log.v("Phone State", "state:"+state);

switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("Phone State", "incomingNumber:"+incomingNumber+" ended");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.v("Phone State", "incomingNumber:"+incomingNumber+" picked up");
break;
case TelephonyManager.CALL_STATE_RINGING:
if(inbounda.equalsIgnoreCase(incomingNumber)){
Log.v("ARE THE SAME...............................", "incomingNumber:"+incomingNumber+ "inbound:"+ inbounda + "MSG TO DISPLAY" +msg);

try{
Class c = Class.forName(myt.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(myt);
telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}

try{
Thread.currentThread().sleep(4000);
replyChecker(MessagetoDisplay);
} catch(InterruptedException ie){

}
}
break;
default:
break;
}
}
}

public String replyChecker(int Check) {
SmsManager mySMS = SmsManager.getDefault();
String destination = inbounda;
//open - to be extended to allow user to define own tailored message
String Meeting = "Sorry I am at a meeting. Graham ";
String Gym = "I am currently at the gym and will respond to you later. Graham ";
String NewYearsEve = "Happy New Year to you and your loved ones. Graham ";

if(Check == 1) {
Log.v("HERE NOW1",":");
reply = Meeting;

mySMS.sendTextMessage(destination, null, Meeting, null, null);
return reply;
} else if (Check == 2) {
Log.v("HERE NOW2",":");
reply = Gym;
mySMS.sendTextMessage(destination, null, Gym, null, null);
return reply;
} else if (Check == 3) {
Log.v("HERE NOW3",":");
reply = NewYearsEve;
mySMS.sendTextMessage(destination, null, NewYearsEve, null, null);
return reply;
}
// TODO Auto-generated method stub
return reply;
}
}

最佳答案

I have multiple activities across my application, and I need a way to keep state in case the phone goes into standby and nativating between activities.

当用户正在使用您的应用程序时,手机不会进入待机状态,并且绝对不会在 Activity 之间,假设任何与正常应用程序流程模糊相似的事情。设备只会在不活动后进入休眠状态,并且当用户点击某物以从一个 Activity 移动到下一个 Activity 时,不活动计时器会重置。

如果您希望在屏幕上进行某些 Activity 时让设备保持唤醒状态,请使用 android:keepScreenOnsetKeepScreenOn()在某些 View 上,因此您不必弄乱自己的 WakeLock

关于android - 无法注销电话状态监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11717999/

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