Java 电话侦听器逻辑
我有一个电话听筒,有两个选择,铃声和空闲 我正在努力实现的目标:Java 电话侦听器逻辑,java,android,Java,Android,我有一个电话听筒,有两个选择,铃声和空闲 我正在努力实现的目标: switch (state) { case CALL_STATE_RINGING: assert notificationManager != null; if (notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY &&
switch (state) {
case CALL_STATE_RINGING:
assert notificationManager != null;
if (notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_ALARMS &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_UNKNOWN &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_NONE) {
notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_NONE);
}
break;
case CALL_STATE_IDLE:
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
assert notificationManager != null;
notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL);
}
switch (state) {
case CALL_STATE_RINGING:
assert notificationManager != null;
if (notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_ALARMS &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_UNKNOWN &&
notificationManager.getCurrentInterruptionFilter() != INTERRUPTION_FILTER_NONE) {
notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_NONE);
}
break;
case CALL_STATE_IDLE:
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
assert notificationManager != null;
notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL);
}
我的尝试:
我尝试在我的第一个开关盒中创建一个布尔值“isActivated”,并将其传递给我的第二个开关盒。如果isActivated为true,则将中断筛选器设置为All。否则,什么也不做。但这没有效果
如果能给我一些建议,我将不胜感激,因为我不明白我的逻辑哪里是错的
编辑:我已经移动了检查,以查看在电话侦听器启动之前“请勿打扰”是否已打开,这样,如果DND已打开,则不会发生任何事情。这似乎有道理。然而,结果并不一致。大多数情况下,当状态变为空闲状态时,中断\u过滤器\u都会启动,有时不会
以下是我的侦听器代码:
@Override
public void onReceive(final Context context, Intent intent) {
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
dnDisturbOn = notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_PRIORITY ||
notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_ALARMS ||
notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_UNKNOWN ||
notificationManager.getCurrentInterruptionFilter() == INTERRUPTION_FILTER_NONE;
if (!dnDisturbOn) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephony != null) {
telephony.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String number) {
super.onCallStateChanged(state, number);
testNumber(context, state, number);
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
是否有任何原因说明代码的现状会影响“testNumber”方法的一致性?我认为应该有三种状态。 1.铃声 2.闲置 3.闲着不打扰
从第一个开始,当您关闭请勿打扰时,您将进入第二个,从第二个开始,当有人呼叫您时,您可以进入第一个,当有人打开请勿打扰时,您可以进入第三个。从第三个开始,当有人关掉请勿打扰时,你们只能在第二个开始。我不知道这是您想要的,还是您只想要两个州。很有趣,谢谢,但您如何实现这些规则?例如,如何确保从第三位开始只能进入第二位?我不知道我们在谈论同一件事,但如果你与状态一起工作,你必须有某种东西能够触发状态的变化。如果你处于第三种状态,并且它开始响,你不会对此做出反应,只是忽略它。这是一种不会改变你状态的东西,你在等待打扰,而不是铃声。当干扰关闭事件发生时,您将改变状态OK,我想我理解。今晚我会试试,如果有用我会告诉你的。谢谢你的帮助!我正在努力解决这个问题。如果不太麻烦的话,你能给我举个例子吗@例如,您在接收事件的函数中更改此项,这是一个伪代码函数(事件){开关(状态)案例振铃:如果(事件类型==干扰打开)状态=空闲案例空闲:如果(事件类型==干扰打开)状态=空闲\不干扰其他if(事件类型==振铃),则执行操作state=Ringing case Idle\u do\u not\u interru:如果(event.type==interru\u off)state=Idle;end switch}再次,我不确定我是否理解您需要做什么。