问题
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?
回答1:
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! :)
回答2:
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
来源:https://stackoverflow.com/questions/8880508/on-android-how-do-i-get-the-phone-number-from-incoming-call