1

I am trying to dial a user from my app launching the dialer in android

I have provided the manifest permission:

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

I have also provided the runtime permission

Code:

private fun startPhoneDial(phoneNo: String) {
        val callIntent = Intent(Intent.ACTION_CALL)
        //callIntent.data = Uri.parse(phoneNo)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            callIntent.setPackage("com.android.phone")
        }else{
            callIntent.setPackage("com.android.server.telecom")
        }
        callIntent.data = Uri.parse("tel:$phoneNo")
        startActivity(callIntent)
    }

Error-Stack-Trace:

Exception: Method threw 'android.content.ActivityNotFoundException' exception.

Message: No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx pkg=com.android.phone }
Devrath
  • 42,072
  • 54
  • 195
  • 297

5 Answers5

3

Try like this.

val callIntent = Intent(Intent.ACTION_DIAL)
callIntent.setData(Uri.parse("tel:" + phone_number));
1

Use this function

fun callANumber(context: Context, phoneNo: String) {
     if (TextUtils.isEmpty(phoneNo)) {
        return
    }

    val callIntent = Intent(Intent.ACTION_CALL)
    callIntent.data = Uri.parse("tel:$phoneNo")
    
    try {
        context.startActivity(callIntent)
    } catch (e: ActivityNotFoundException) {
        Toast.makeText(context,"No Activity found which can handle intent",Toast.LENGTH_LONG).show()
    }

}
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • CALL_PHONE is not needed to open the dialer. It's only needed when you want to call directly without user's interaction. – Borja Feb 16 '21 at 16:21
1

I would suggest you to remove that hardcoded package instead just use the generic intent for opening the dialer as this will call the available intent instead of given one.

Or you just place this below code after handling the activity not found exception.

private fun startPhoneDial(phoneNo: String) {
        val callIntent = Intent(Intent.ACTION_CALL)
        //callIntent.data = Uri.parse(phoneNo)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            callIntent.setPackage("com.android.phone")
        }else{
            callIntent.setPackage("com.android.server.telecom")
        }
        callIntent.data = Uri.parse("tel:$phoneNo")
        try{
        startActivity(callIntent)        
        }
        catch(exp : ActivityNotFoundException){
         val intent = new Intent(Intent.ACTION_DIAL);
         intent.setData(Uri.parse("tel:${phoneNo}"));
         startActivity(intent); 

        }
    }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
1

We can call directly ACTION_DIAL.

            val phone = "+919898989898"
            val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null))
            startActivity(intent)

reference:- https://stackoverflow.com/a/18973484/2553615

Mohit
  • 11
  • 2
0

Because it's the opposite way... In versions smaller than lollipop, it's:

com.android.phone

From versions starting at lollipop, it's:

com.android.server.telecom

So you should just swap your greater-than sign like:

private fun startPhoneDial(phoneNo: String) {
    val callIntent = Intent(Intent.ACTION_CALL)
    //callIntent.data = Uri.parse(phoneNo)
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
        callIntent.setPackage("com.android.phone")
    }else{
        callIntent.setPackage("com.android.server.telecom")
    }
    callIntent.data = Uri.parse("tel:$phoneNo")
    startActivity(callIntent)
}