我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
无论我是否转动手机,都会执行条件下的代码。
我究竟做错了什么?有没有其他方法可以达到这个目的?