0

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);

Sagar Dhawan
  • 47
  • 1
  • 9

1 Answers1

0

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.

Nilesh Panchal
  • 1,059
  • 1
  • 10
  • 24
  • 1
    I have used the above code also but it is not working on all devices. – Sagar Dhawan Sep 20 '19 at 10:01
  • 1
    @SagarDhawan user [TelecomManager](https://developer.android.com/reference/kotlin/android/telecom/TelecomManager) instead of TemephonyManager for latest os version and for the old current one. – Nilesh Panchal Sep 20 '19 at 10:05