我正在尝试将以下内容集成到我的代码中以取消注册 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;
}
}