在 Android 上如何从来电中获取电话号码?

发布于 2024-12-27 01:31:45 字数 757 浏览 2 评论 0 原文

我正在尝试获取来电的“incoming_number”。我搜索并找到这篇文章,但它已经过时了,我不能'似乎无法与 Google 最新版本的 Android (4.0.3) 配合使用。

阅读另一篇文章后,似乎触发呼叫传入的最简单(也可能是唯一)的方法是设置一个基于 PhoneStateListener 做出反应的 BroadcastReceiver,提到的: 约翰·费米内拉

当我以同样的方式设置它时,它会显示在 jakob 的帖子中,调试器永远不会落入 onCallStateChanged () 方法,并且我已经做了 jakob 的注释中提到的修改post.. (onCallStateChange"d"())

但是,我仍然没有运气,我认为这种获取传入号码的方法已被弃用,并且在 ICS (4.0.3) 中不起作用。还有其他传统或半传统的方法可以实现这一目标吗?

I am trying to grab the "incoming_number" of an incoming phone call. I searched and found this post, but it's outdated and I can't seem to get this to work with Google's latest version of Android (4.0.3).

After reading the other post it seems that the easiest (and possibly only) way to trigger that a call is coming in is to set a BroadcastReceiver that reacts based on the PhoneStateListener, mentioned by: John Feminella.

When I set this up the same way it is displayed in the post by jakob the debugger never drops into the onCallStateChanged() method, and I have made the modifications that were mentioned in the remarks in jakob's post.. (onCallStateChange"d"())

However, I still am getting no luck and I figured that this method of grabbing the incoming_number has been deprecated and not working in ICS (4.0.3). Are there any other conventional or semi-conventional ways to achieve this?

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

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

发布评论

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

评论(2

冷情 2025-01-03 01:31:45

为了让它在较新版本的 Android(4.0.3 等)中工作,您需要确保您的 minSdkVersion3。我的代码的问题是我的 minSdkVersion7..

希望这可以帮助其他试图解决这个问题的人! :)

In order to get this to work in newer versions of Android (4.0.3 etc) you need to make sure that your minSdkVersion is 3.. The issue with my code was that my minSdkVersion was 7..

Hope this helps others trying to figure this out! :)

凤舞天涯 2025-01-03 01:31:45

jakob 没有提到的是,您必须在使用 PhoneStateListener 之前注册它。在您想要拦截来电号码之前,将以下代码放置在某个位置(例如,在您的活动的 onCreate 方法中):

TelephonyManager manager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
manager.listen(new CustomPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);

然后应该触发 onCallStateChanged 方法。我不知道这个方法在 4.0.3 中已被弃用。但我认为作为一个快速解决方案,这应该还是可以的。

希望对您有所帮助。
干杯

What jakob does not mention is that you have to register the PhoneStateListener prior to use it. Put the following code somewhere before you want to intercept the incoming calls number (e.g. in the onCreate method of your activity):

TelephonyManager manager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
manager.listen(new CustomPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);

Then the onCallStateChanged method should be triggered. I didn't know that this method is deprecated in 4.0.3. But I think as a quick solution this should still be fine.

Hope to have helped you.
Cheers

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