电话状态监听器总是执行代码

时间:2013-09-20 13:07:26

标签: android android-intent phone-state-listener

我在我的应用中设置了phoneStateListener,当来电期间手机倒置时,手机会静音。问题是手机无论有什么条件都会沉默。这是我的代码 -

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
TelephonyManager telephonyManager;
PhoneStateListener phoneStateListener;
telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
phoneStateListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
                // callStateStr = "ringing. Incoming number is: "
                // + incomingNumber;

                if (z1 > -10 && z1 < -9) {//this is to detect if the phone is turned upside down or not. consider this defined.

                    am.setStreamVolume(AudioManager.STREAM_RING, 0,
                            AudioManager.FLAG_ALLOW_RINGER_MODES);
                    Log.d("abc", "turning silent");
                    Toast.makeText(getApplicationContext(), "turning silent",Toast.LENGTH_SHORT).show();
                }
                break;
}
}
};

在来电期间,无论我是否转动手机,都会执行if条件下的代码。

我做错了什么?有没有其他方法可以达到这个目的?

0 个答案:

没有答案