Retrieve incoming call's phone number in Android
Asked Answered
T

3

46

I would like to retrieve the incoming call's phonenumber and do something with it like the do in http://blog.whitepages.com/2009/02/27/caller-id-by-whitepages-a-new-android-app-that-puts-telemarketers-on-alert/

Could you please help me because I can't find any information about this. Where do i start and how do i get hold of the phonenumber?


Ok so currently my code looks like below. When I place the call the CustomBroadcastReceiver catches it and the log message is printed out. I can retrieve the telephone number from the bundle. But! I can't get hte CustomPhoneStateListener to work. As you can see I have registered my customPhoneState listener to the receiver but the log message never get's printed out from the CustomPhoneStateListener class. What am I my missing here? Is my thinking correct?


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

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

public void onCallStateChange(int state, String incomingNumber){

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    Log.v(TAG, incomingNumber);

    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(TAG, "RINGING");
            break;
    }   
}

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


    Bundle bundle = intent.getExtras();
    String phoneNr= bundle.getString("incoming_number");
    Log.v(TAG, "phoneNr: "+phoneNr);

}
Tibiotarsus answered 5/12, 2009 at 19:42 Comment(1)
I am not getting incoming_number for incoming call Device POCO f1 pi and receiver is getting called twiceDuenna
Q
25

Use PhoneStateListener. It has an onCallStateChanged handler; one of the supplied arguments you'll get is a String containing the incoming phone number.

Quevedo answered 5/12, 2009 at 19:48 Comment(5)
Could you please provide an example implementation?Tibiotarsus
My answer for this question may help linkPresentment
I got nothing in the incomingNumber param, what should it be?Spokesman
same thing here, not getting phoneCourtland
This class was deprecated in API level 31.Racer
A
5

Your overridden method in CustomPhoneStateListener should be called onCallStateChanged() (and not onCallStateChange()).

This would have been spotted by the Java compiler if you would have had the @Override annotation, like you have for onReceive().

Andesite answered 6/12, 2009 at 19:23 Comment(2)
You are the man! Thanks! I have one more question to ask and that is how do i add stuff to the incoming call view?Tibiotarsus
I'm not sure about the specifics of that one. Your typo was easier :) However, I think what you are looking for is a Toast. But it will probably not show on the incoming call screen unless your code is a service (and not an activity). But as I said, I haven't gotten this far in Android development myself yet. developer.android.com/guide/topics/ui/notifiers/toasts.htmlAndesite
H
4

The above answers are out-od-dated now. There are valid for Android 7 and lower.

For android 9 and higher you have to add another permission in the Androidmanifest.xml with the permission

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

without the phone number will be null. For Android 8 I am not sure.

PhoneStateReciever.java

    package com.incomingcalls;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.Toast;


    public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            System.out.println("Receiver start");
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.e("Incoming Number", "Number is ," + incomingNumber);
            Log.e("State", "State is ," + state);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
                Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();


            }
            if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
                Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
            }
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
}

AnroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.incomingcalls">
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".PhoneStateReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Hilton answered 30/12, 2020 at 6:27 Comment(1)
<meta-data android:name="flutterEmbedding" android:value="2" /> did you delete this line?Quach

© 2022 - 2024 — McMap. All rights reserved.