18

I have been using the following code in a BroadcastReceiver to get the caller ID of incoming calls:

@Override
public void onReceive(Context aContext, Intent aIntent) {
   String action = aIntent.getAction();

   if (action==null) return;
   if (!action.equals("android.intent.action.PHONE_STATE")) return;

   String curState = aIntent.getStringExtra(TelephonyManager.EXTRA_STATE);

   if ((TelephonyManager.EXTRA_STATE_RINGING.equals(curState))
      &&(TelephonyManager.EXTRA_STATE_IDLE.equals(oldState)))){
      String incNumber = aIntent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

      // do something here
   }
   oldState=curState;
}

Unfortunately this has stopped working in Android 9.0 (API 28). More specifically, aIntent.getStringExtra(EXTRA_INCOMING_NUMBER) always returns null. In android versions<=API 27 everything works correctly

I have also added the READ_PHONE_STATE and READ_CALL_LOG permissions in the manifest file.

Any ideas? Anybody else experiencing the same problem?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
haris
  • 481
  • 4
  • 9

2 Answers2

29

I found the answer to my question:

First, in Android 9, you have to explicitly ask for both the READ_PHONE_STATE and the READ_CALL_LOG permissions at run time. In previous Android versions you only had to ask for the READ_PHONE_STATE permission. Both of them have to be asked at run time.

Second, if both of the above permissions have been given, the onReceive method is called twice (!!). The first time the intent is "empty" (EXTRA_INCOMING_NUMBER is null). The second time the intent is normally populated as it should. This is documented in the TelephonyManager Documentation.

haris
  • 481
  • 4
  • 9
  • Is your broadcast receiver call when application is closed ?? I am facing this issue. Please post your whole receiver. – RushDroid Dec 16 '18 at 04:51
  • Yes, the receiver was called. https://android-developers.googleblog.com/2018/10/providing-safe-and-secure-experience.html – haris Dec 17 '18 at 08:16
  • 2
    Sorry, the previous comment was posted without having finished it. I wanted to bring to your attention the new google policy, effective January 9, 2019, which restricts the use of the use of the READ_CALL_LOG permission (see the link in the previous comment). – haris Dec 17 '18 at 08:24
  • 3
    As @haris wrote, since January 9, 2019 you cant ask for READ_CALL_LOG if your application is not default dialler app. How can I get caller id without this permission on api 28 and above? – spajdo Jan 14 '19 at 08:56
  • Read call logs permission is restrict by google developer console. You can not request request this permission.You need to fill up the form for access the permission and if they agree than only you can use this permission – RushDroid Mar 19 '19 at 11:45
  • For me it's always null on Android9. Anyone? – powder366 Apr 11 '19 at 13:43
  • @spajdo i wonder how this app is doing it then : https://play.google.com/store/apps/details?id=com.triputiapps.nameannouncer, they are not default dialer – j2emanue May 03 '19 at 09:25
  • @j2emanue I don't know. Maybe he got exception, or his application will not work on Android 9 or higher. I don't have Android 9 yet, but someone can test it. – spajdo May 06 '19 at 08:16
  • Did you get any solution? I have aske for both permission at runtime. Still incomingNumber is null. – GreenROBO Jun 07 '19 at 09:22
  • Thanks a lot.. it is working. I got an error on first onReceive call, I put condition to execute the next line only if incoming number is not null, then automatically called onReceive call again, fortunately, got the incoming number .. – Suhad Bin Zubair Sep 20 '19 at 03:16
  • @haris can you publish this application in google play store? If so please help me to publish this application. When i add this read call log permission they don't publish my app. – pavel Sep 21 '19 at 05:56
  • @pavel, My app has been also removed from google play store (It had been there form almost 10 years, with many users :-( ). I distribute it now as an apk from my website. – haris Sep 22 '19 at 06:18
  • can you tell me the full process about your distribution process. – pavel Sep 22 '19 at 12:53
  • not getting the phone_state broadcast when app is closed on android 9. any suggestions !!! – Sagar Nayak Jan 15 '20 at 10:15
4

Add READ_CALL_LOG permission in manifest as below :

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

Then

Apps -> Your App -> Permissions -> Grant Call Logs Permission

Your should get phone number now, after the second time your receiver is called

Android
  • 1,420
  • 4
  • 13
  • 23