电话听众逻辑

时间:2018-04-29 21:08:01

标签: java android

我有一个手机监听器,有2个选项,振铃和闲置。

我想要实现的目标:

  1. 如果请勿打扰。 电话响了。 电话响铃时请勿打扰。 手机停止响了。 请勿打扰。

  2. 如果请勿打扰。没有任何事情发生。

  3. 实际发生的事情:

    1. 工作正常。
    2. 当手机停止响铃时,请勿打扰。
    3. 我的代码示例:

      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"在我的第一个Switch案例中,并将其传递给我的第二个Switch案例。如果isActivated为true,则将中断过滤器设置为All。否则,什么也不做。但这没有效果。

      我会感激一些指示,因为我不明白我的逻辑错在哪里。

      编辑:我已经移动检查以查看在电话收听器启动之前是否打开了“请勿打扰”,以便在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"?

1 个答案:

答案 0 :(得分:0)

我认为你应该有三个州。 1.Ringing 2.Idle 3.Idle_do_not_disturb

首先,当你关闭请勿打扰时,你将进入第二,当你有人打电话给你时,你可以先进入第二,当有人打开时,你可以先进入第三,请勿打扰。从第三个开始,当有人关闭请勿打扰时,你只能在第二个。我不知道这是你想要的,或者只想要两个州。

相关问题