2

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

2 回答 2

1

我的应用程序中有多个活动,我需要一种方法来保持状态,以防手机进入待机状态并在活动之间进行导航。

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

如果您希望在屏幕上显示某些活动时保持设备唤醒,请使用android:keepScreenOnsetKeepScreenOn()在某些活动上View,这样您就不必弄乱自己的WakeLock.

于 2012-10-16T21:18:15.850 回答
1

实际上,我通过在服务中实现广播接收器解决了这个问题。我面临的问题与 PowerManager 无关,而是该服务没有监听来电。

监听 android.intent.action.PHONE_STATE 的广播接收器完成了这项工作

谢谢格雷厄姆

于 2012-10-21T17:13:42.870 回答