Search code examples
androidphone-calltelephonymanager

How to request for call logs permission for my android application


I am creating an application which performs a certain set of operation for the calls i receive or make.But the application is not able to detect the caller id even though i have provided the required permissions.

My Manifest:

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

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

My BrodcastReciever:

  TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
                telephonyManager.listen(new PhoneStateListener(){
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        super.onCallStateChanged(state, incomingNumber);
                        System.out.println("incomingNumber : "+incomingNumber);
                        if(state == TelephonyManager.CALL_STATE_IDLE && clientContacts.containsKey(incomingNumber)) {
                            sharedPreferences.edit()
                                    .putString("callerName",clientContacts.get(incomingNumber))
                                    .apply();

                            final Intent intent1 = new Intent(context, ClientInteractionDialogService.class);
                            intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            new Handler().postDelayed(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    ContextCompat.startForegroundService(context, intent1);
                                }
                            },500);
                        }
                    }
                },PhoneStateListener.LISTEN_CALL_STATE);


Solution

  • The following code will give you first-time null value but after that, it will always give you the correct number

    Take the run time permissions from the user.

    For getting phone number in your broadcast receiver you need to implement following code

    IncomingCallReceiver.kt

    class IncomingCallReceiver : BroadcastReceiver() {
    
    
        override fun onReceive(context: Context?, intent: Intent?) {
    
            try {
                val newPhoneState = intent!!.getStringExtra(TelephonyManager.EXTRA_STATE)
                val bundle = intent.extras
    
                if (newPhoneState != null && newPhoneState == TelephonyManager.EXTRA_STATE_RINGING) {
                    val phoneNumber = bundle!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER)
    
                    if (phoneNumber != null) {
                        Toast.makeText(context, "Ring $phoneNumber", Toast.LENGTH_SHORT).show()
                    }
                 }
           } catch (ee: Exception) {
                Log.i("Telephony receiver", ee.message!!)
            }
        }
    
    }
    

    Register receiver in your manifest file

    <receiver
                android:name="com.nip.callblock.IncomingCallReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />
    
                </intent-filter>
            </receiver>
    

    This will give you the incoming number in onReceive Method

    Read this carefully may you will get the solution.

    If you found any issue then contact me.