我有一个电话听者有两个选择,铃声和空闲。
我想要达到的目标:
实际发生了什么:
我的代码示例:
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为真,则将中断筛选器设置为All。否则什么都别做。但这没有效果。
如果我不知道我的逻辑哪里错了,我会很感激你给我一些指点。
编辑:我已经移动了检查,看看在电话侦听器启动之前是否打开了DND,所以如果DND已经打开,就不会发生任何事情。这似乎很有道理。然而,结果并不一致。大多数情况下,当状态处于空闲状态时,INTERRUPTION_FILTER_ALL就会启动,偶尔也不会。
下面是我的侦听器代码:
@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“方法的一致性?
发布于 2018-04-29 21:56:06
我觉得你应该有三个州。1.振铃2. 2.Idle 3.Idle_do_not_disturb
从第一次到第二次,当你关机时不要打扰;从第二次开始,当有人打电话给你时,你可以先进去;第三次,当有人打开时,不要打扰。从第三,你只能在第二,当有人关闭时,不要打扰。我不知道这是你想要的,还是你只想要两个州。
https://stackoverflow.com/questions/50091336
复制相似问题