4

I am trying to get incoming call info using broadcast receivers on android studio. following are the methods i tried:

<receiver android:name=".receivers.IncomingCalls">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

On this receiver's intent i tried:

Method one:

incnumber=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

Method two:

final String state1 = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, final String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                incnumber=incomingNumber;
            }
        },PhoneStateListener.LISTEN_CALL_STATE);

Method Three:

incnumber=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

All the three methods return empty incnumber.

By the way, I am running this project on android pie, Please help me out.

7
  • is it working fine for all other versions except pie ? Commented Mar 1, 2019 at 6:03
  • I think not , I will try and revert asap
    – AruN
    Commented Mar 1, 2019 at 6:04
  • ok..Have you added this permission <uses-permission android:name="android.permission.READ_PHONE_STATE"/> Commented Mar 1, 2019 at 6:06
  • It seems it's only for pie, it works on oreo
    – AruN
    Commented Mar 1, 2019 at 6:20
  • yep i've done that
    – AruN
    Commented Mar 1, 2019 at 6:20

1 Answer 1

9

Try this way... permission

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

Make a Broadcast receiver say ServiceReceiver assign its action in Manifest.

<receiver android:name=".ServiceReceiver" >
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

ServiceReceiver.Java

public class ServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            System.out.println("incomingNumber : "+incomingNumber);
        }
    },PhoneStateListener.LISTEN_CALL_STATE);
}

}

1
  • Can we get the number from incoming call on android 11 without call-logs permission??? or from notification listner??? Commented Feb 18, 2022 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.