其他分享
首页 > 其他分享> > android-无法读取手机状态

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);

标签:telephonymanager,android
来源: https://codeday.me/bug/20191101/1986721.html