0

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?

Community
  • 1
  • 1
DizzyThermal
  • 104
  • 1
  • 10

2 Answers2

2

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! :)

DizzyThermal
  • 104
  • 1
  • 10
0

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

Dude
  • 652
  • 2
  • 10
  • 24
  • I'm pretty sure that in the **CallBroadcastReceiver** class, that [jakob](http://stackoverflow.com/users/247071/jakob) made, he uses: `CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(); telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);` However, it's still not reaching this point. I want to ultimately have this in a Service and send a text message to the incoming number, still having no luck.. – DizzyThermal Jan 16 '12 at 22:08
  • Yes, but he registers the PhoneStateListener in the onReceive Method of the BroadcastReceiver. This means, it is registered when the BroadcastReceiver is triggered. As it is triggered when the call arrives, the PhoneStateListener is registered after the phone call has arriven. And then the PhoneStateListener is not triggered, because it only listens to state changes and not if there is an ongoing call. Maybe it is triggered again when the call is hung up. But better register the PhoneStateListener in the onCreate Method or somewhere similar, and it should work fine. – Dude Jan 16 '12 at 22:33
  • When I try to register the PhoneStateListener in the **onStart(Intent intent, int startId)** portion of my service it crashes the app.. It is defined exactly as you said and it is also defined in the Manifest.. – DizzyThermal Jan 16 '12 at 23:17
  • There's actually a ton of them.. However, I feel like the Intent for the BroadcastReceiver might be wrong.. It never enters the BroadcastReceiver.. Therefore I would _think_ that it is not properly being triggered, no? Here are my receivers: `registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));` `registerReceiver(resend, new IntentFilter("SMS_SENT"));` `registerReceiver(callReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));` *Sorry for the awkward line breaks.. >.> – DizzyThermal Jan 17 '12 at 18:10
  • I ended up getting it.. I needed to drop my minSdkVersion from 7 to 3 and it worked.. So clearly it is deprecated in newer versions of Android.. :-\ Thanks for the help @Dude.. – DizzyThermal Jan 17 '12 at 23:35
  • thanks for the help though.. I figured after awhile I was coming off as a pest, but it was a problem that completely halted development.. – DizzyThermal Jan 21 '12 at 10:21
  • Hello @DizzyThermal, I am trying to get the "phone number/incoming number" of incoming call. My problem is in version above 4.0 I am not able to get the "phone number/incoming number" it come to blank. As per your answer I tried to change the minSdkVersion to 3 but it didn't help me. – Dory Sep 27 '13 at 11:38