android-无法读取手机状态

我想在来电时在应用程序中执行一些操作(暂停游戏).但是读取电话状态无效.我在清单中授予了权限(READ_PHONE_STATE).来电时没有任何反应.
谢谢.

TelephonyManager telephonyManager;
PhoneStateListener listener;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
*
*
*
listener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Toast.makeText(SudokuGameActivity.this, "IDLE", Toast.LENGTH_SHORT).show();
          break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
         Toast.makeText(SudokuGameActivity.this, "OFF Hook", Toast.LENGTH_SHORT).show();
          break;
        case TelephonyManager.CALL_STATE_RINGING:
            Toast.makeText(SudokuGameActivity.this, "Ringing", Toast.LENGTH_SHORT).show();
            mpauseButton.performClick();
          break;
          }

        }
       };

解决方法:

您是否写过以下几行:

 telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
上一篇:Android Studio3.5版本电话监听器出现telephonymanager.services.ListenService$MyListen.onCallStateChanged错误


下一篇:中断Android中的来电