读取手机状态时出现问题

发布于 2024-12-25 03:46:40 字数 967 浏览 2 评论 0原文

当来电时,我想在我的应用程序中执行一些操作(暂停游戏)。但读取手机状态不起作用。我已在清单中授予权限(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;
          }

        }
       };

I want to perform some operation (Pause game) in my application when a call came. But reading the phone state is not working. I have given permission(READ_PHONE_STATE) in the manifest. Nothing is happen when a call came.
Thanks.

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

        }
       };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2025-01-01 03:46:40

您是否写过以下行:

 telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);

Have you written the following line :

 telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
过潦 2025-01-01 03:46:40

创建监听器后,您需要调用“public void Listen(PhoneStateListenerlistener, int events)”来监听。

另外,你可以尝试这个:
创建一个broadcatst接收器处理动作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());
}

}

when your listener has be created, you need invoke `public void listen (PhoneStateListener listener, int events)' to listen.

also, you can try this:
create a broadcatst receiver handle the action android.intent.action.PHONE_STATE,

code example:

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文