Android 电话状态侦听器始终执行代码
我已经在我的应用程序中设置了Android 电话状态侦听器始终执行代码,android,android-intent,phone-state-listener,Android,Android Intent,Phone State Listener,我已经在我的应用程序中设置了phoneStateListener,当手机在来电过程中倒置时,它会使手机静音。问题是,无论何种情况,手机都会静音。这是我的密码- AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); TelephonyManager telephonyManager; PhoneStateListener phoneStateListener; telephonyManager = (Te
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;
}
}
};
AudioManager am=(AudioManager)getSystemService(Context.AUDIO\u SERVICE);
电话管理器电话管理器;
PhoneStateListener PhoneStateListener;
telephonyManager=(telephonyManager)此
.getSystemService(上下文.电话服务);
phoneStateListener=新的phoneStateListener(){
public void onCallStateChanged(int状态,字符串incomingNumber){
super.onCallStateChanged(状态、收入编号);
开关(状态){
case TelephonyManager.CALL_STATE_响铃:
//callStateStr=“振铃。传入号码为:”
//+收入编号;
如果(Z1> -10和& Z1<9){//这是为了检测电话是否颠倒过来。考虑这个定义。
am.setStreamVolume(AudioManager.STREAM_环,0,
AudioManager.FLAG_ALLOW_RINGER_MODES);
日志d(“abc”,“沉默”);
Toast.makeText(getApplicationContext(),“保持沉默”,Toast.LENGTH\u SHORT.show();
}
打破
}
}
};
在来电过程中,无论我是否打开手机,都会执行if
条件下的代码
我做错了什么?有没有其他方法可以达到这个目的?你从哪里得到
z1
?这段代码放在onSensorChanged
回调中不是更好吗?@323go-这段代码只在onSensorChanged
回调中。而z1
是SensorEvent
的z轴值。我已经避免了一些代码,以使代码看起来整洁。@323我应该发布整个代码吗?