阅读手机状态有问题

时间:2012-01-05 09:14:40

标签: android telephonymanager

我希望在来电时在我的应用程序中执行一些操作(暂停游戏)。但阅读手机状态无效。我在清单中给了许可(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;
          }

        }
       };

2 个答案:

答案 0 :(得分:3)

您是否写过以下内容:

 telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);

答案 1 :(得分:1)

当你的监听器被创建时,你需要调用`public void listen(PhoneStateListener listener,int events)'来监听。

另外,你可以试试这个: 创建一个广泛的接收器来处理动作android.intent.action.PHONE_STATE

代码示例:

public class PhoneStateReceiver extends BroadcastReceiver {

private TelephonyManager manager;

@Override
public void onReceive(Context context, Intent intent) {
    if (manager == null) {
        manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    String action = intent.getAction();
    System.out.println(action);
    System.out.println("current phone state:" + manager.getCallState());
}

}