无法注销phonestatelistener [英] unable to unregister phonestatelistener
问题描述
我试图整合以下到我的code注销phonestatelistener
TelephonyManager经理=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
如果(经理!= NULL){
mgr.listen(phoneStateListener,PhoneStateListener.LISTEN_NONE);
}
我有在后台运行,并检查一个数字是阻止列表上的服务。如果是的话那么它会调用 PhonestateListener
TelephonyManager MTM =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(新phoneStateListener(NumberInbound,Msg_to_display,MTM),PhoneStateListener.LISTEN_CALL_STATE);
但是当我从阻止列表中删除了一些我想注销 phonestatelistner
。相反,什么情况是, phonestatelistener
总是被注册了MSISDN我previously删除仍然受阻!
你能帮忙吗?
$ C $下 phonestatelistner
类phoneStateListener扩展PhoneStateListener {
字符串inbounda;
INT MessagetoDisplay;
弦乐味精;
字符串的答复;
TelephonyManager MYT;
为MyService calltoService;
布尔mBound = FALSE;
私有静态最后字符串变量=PHONELISTENER;
phoneStateListener(串入,整型消息,TelephonyManager T){
inbounda =入境;
MessagetoDisplay =消息;
MYT = T;
}
公共无效onCallStateChanged(INT状态,串incomingNumber){
如果(!incomingNumber.equals(inbounda)){
} 其他 {
最终的字符串变量=电话呼叫;
ITelephony telephonyService;
//新的code
Log.v(手机状态,状态:+状态);
开关(州){
案例TelephonyManager.CALL_STATE_IDLE:
Log.v(手机状态,incomingNumber:+ incomingNumber +端);
打破;
案例TelephonyManager.CALL_STATE_OFFHOOK:
Log.v(手机状态,incomingNumber:+ incomingNumber +捡到);
打破;
案例TelephonyManager.CALL_STATE_RINGING:
如果(inbounda.equalsIgnoreCase(incomingNumber)){
Log.v(是相同的...............................,incomingNumber:+ incomingNumber +入站: + inbounda +味精,显示+味精);
尝试{
C类=的Class.forName(myt.getClass()的getName());
方法米= c.getDeclaredMethod(getITelephony);
m.setAccessible(真正的);
telephonyService =(ITelephony)m.invoke(MYT);
telephonyService.silenceRinger();
telephonyService.endCall();
}赶上(例外五){
e.printStackTrace();
}
尝试{
Thread.currentThread()睡眠(4000)。
replyChecker(MessagetoDisplay);
}赶上(InterruptedException的IE){
}
}
打破;
默认:
打破;
}
}
}
公共字符串replyChecker(INT检查){
SmsManager的mySMS = SmsManager.getDefault();
字符串目标= inbounda;
//开 - 被扩展,以允许用户能够定义自己的定制消息
字符串会议=对不起,我是在一次会议格雷厄姆。
字符串健身房=我目前在健身房,并会回应你以后格雷厄姆。
字符串NewYearsEve =新年快乐你和你所爱的人格雷厄姆。
如果(检查== 1){
Log.v(这里NOW1,:);
回复=会议;
mySMS.sendTextMessage(目的地,空,会议,NULL,NULL);
返回答复;
}否则,如果(检查== 2){
Log.v(这里NOW2,:);
回复=健身房;
mySMS.sendTextMessage(目的地,空,健身房,NULL,NULL);
返回答复;
}否则,如果(检查== 3){
Log.v(这里NOW3,:);
回复= NewYearsEve;
mySMS.sendTextMessage(目的地,空,NewYearsEve,NULL,NULL);
返回答复;
}
// TODO自动生成方法存根
返回答复;
}
}
解决方案
其实,我通过实现服务的广播接收机得到了解决此问题
一个广播接收器监听android.intent.action.PHONE_STATE完成工作
由于格雷厄姆
I am trying to integrate the following into my code to unregister phonestatelistener
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
I have a service which runs in the background and checks if a number is on a blocking list. If yes then it calls PhonestateListener
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(new phoneStateListener(NumberInbound,Msg_to_display,mTM), PhoneStateListener.LISTEN_CALL_STATE);
but when i remove a number from the blocking list I want to unregister phonestatelistner
. Instead what happens is that phonestatelistener
is always registered and the msisdn which I previously removed is still blocked !
can you help ?
Code for 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;
}
}
Actually, I got around this problem by implementing a broadcast receiver in a service. The problem I was facing was not to do with PowerManager, but that the service was not listening for incoming phone calls.
A broadcast receiver listening for android.intent.action.PHONE_STATE done the job
Thanks Graham
这篇关于无法注销phonestatelistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!