android - 无法取消注册phonestatelistener

标签 android android-intent

我正在尝试将以下内容集成到我的代码中以注销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 - 无法取消注册phonestatelistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11717999/

相关文章:

java - 使用 Parcelable | 使用 Intent 从一个 Activity 向另一个 Activity 发送列表对象安卓

android - 为什么从一项 Activity 传递可分包数据并在另一项 Activity 中接收部分修改的数据?

android - 递归调用getDatabase

android - 如何一次组合多个解析查询

android - 使用没有 surfaceview 或 textureview 的相机

java - 为什么要同时使用 Intent.FLAG_ACTIVITY_NEW_TASK 和 Intent.FLAG_ACTIVITY_SINGLE_TOP?

android - 如何启动第三方应用程序的 Intent ?

java - 我应该使用哪个 Android/Java 集合来排序键值对,并返回给定点之后的元素

android - 使用 picasso : out of memory exception 从 firebase 存储下载图像

android - Proguard 与 Parceler 和 Realm