无法从 PhoneStateListener::onCallStateChanged 获取非空字符串

发布于 2024-10-07 05:28:36 字数 713 浏览 7 评论 0原文

来源:

listener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
            toast.show();
        }

    };

    ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(listener , PhoneStateListener.LISTEN_CALL_STATE )

嗨,
我正在使用此代码,并具有 android.permission.READ_PHONE_STATE 权限,来获取包含已开始通话次数的 Toast 消息。我的问题是传入号码为空,无论呼叫是拨出还是拨入,都出现 toast,但为空。
我已经看到了一些解决方案,但解决方案是用完全不同的方式获取号码,我想知道我的代码是否可以满足我的需求。
感谢您的建议,
亚当

Source:

listener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
            toast.show();
        }

    };

    ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(listener , PhoneStateListener.LISTEN_CALL_STATE )

Hi,
I am using this code, with android.permission.READ_PHONE_STATE permission, to get toast message including number of started call. My problem is that incomingNumber is empty, it doest not matter if call is outgoing or incomming, toast appear, but empty.
I have seen some solutions for this, but solution was to get the number with completely different way and I wanna know if my code can be used for my needs.
Thanks for advices,
Adam

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-10-14 05:28:36

请在您的申请中包含许可。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />


if(state == TelephonyManager.CALL_STATE_RINGING){
Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
toast.show();
}

CALL_STATE_OFFHOOK 如果第一次调用则返回 null。因此您应该使用CALL_STATE_RINGING

希望它能帮助你。

please include permission in your application.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />


if(state == TelephonyManager.CALL_STATE_RINGING){
Toast toast = Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_LONG);
toast.show();
}

CALL_STATE_OFFHOOK return null if its first call. So you should use CALL_STATE_RINGING.

Hopefully it will help you.

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