Getting the Caller ID in Android 9

Getting the Caller ID in Android 9

In Android 9 (Pie), obtaining the caller ID has been significantly restricted for security and privacy reasons. Direct access to the phone’s call log is no longer granted to apps. Instead, you need to rely on the TelephonyManager and the CallLog.Calls API to get the caller ID information.

Using TelephonyManager

The TelephonyManager is a system service that provides information about the phone’s telephony capabilities. You can use this class to retrieve the caller ID of an incoming call.

Steps to obtain the caller ID:

  • Get an instance of TelephonyManager.
  • Use the getCallerId() method to get the caller ID.

Code Example:

import android.telephony.TelephonyManager;
// ... other imports ...

public class MainActivity extends AppCompatActivity {

    // ... other methods ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        // Retrieve the caller ID using the getCallerId() method
        String callerId = telephonyManager.getCallerId();

        // Display the caller ID in a TextView or any other UI element
        TextView callerIdTextView = findViewById(R.id.caller_id_text_view);
        callerIdTextView.setText(callerId);
    }

}

Using CallLog.Calls API

The CallLog.Calls API provides access to the call log database, enabling you to retrieve call information, including the caller ID, from past calls.

Steps to obtain the caller ID:

  • Get an instance of the ContentResolver.
  • Use the query method to fetch the call log data.
  • Extract the caller ID from the retrieved data.

Code Example:

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog.Calls;
// ... other imports ...

public class MainActivity extends AppCompatActivity {

    // ... other methods ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ContentResolver contentResolver = getContentResolver();

        // Query the call log for the last incoming call
        Cursor cursor = contentResolver.query(
                Calls.CONTENT_URI,
                null,
                Calls.TYPE + " = " + Calls.INCOMING_TYPE,
                null,
                Calls.DATE + " DESC LIMIT 1"
        );

        // Extract the caller ID from the cursor
        if (cursor != null && cursor.moveToFirst()) {
            int callerIdColumnIndex = cursor.getColumnIndex(Calls.NUMBER);
            String callerId = cursor.getString(callerIdColumnIndex);

            // Display the caller ID in a TextView or any other UI element
            TextView callerIdTextView = findViewById(R.id.caller_id_text_view);
            callerIdTextView.setText(callerId);

            cursor.close();
        }
    }

}

Comparing TelephonyManager and CallLog.Calls API

Feature TelephonyManager CallLog.Calls API
Data Access Real-time data for active call Historical data for past calls
Permission Requirement READ_PHONE_STATE READ_CALL_LOG
Use Cases Displaying the caller ID during an incoming call Retrieving caller ID from past call logs

Important Note:

Remember that the information provided in this article is for illustrative purposes and may require adaptation for your specific needs. Please consult the official Android documentation for comprehensive and up-to-date information on using these APIs.


Leave a Reply

Your email address will not be published. Required fields are marked *