Unnati
Unnati

Reputation: 2457

Make Call in Android Application using default call application

I am using following code to make a call from my android application:

Intent intent = new Intent(Intent.ACTION_CALL);                 
intent.setData(Uri.parse("tel:9898989898"));
startActivity(intent);

This opens Intent Chooser if Skype is installed in phone. What i want is it should directly make call from default call application.

How can I achieve this?

Upvotes: 10

Views: 8378

Answers (4)

Amanpreet Kaur
Amanpreet Kaur

Reputation: 512

This is the Kotlin version of @COvayurt code, for rest of the code you can follow from his answer.

                        val callIntent = Intent(Intent.ACTION_CALL)
                        callIntent.data = Uri.parse("tel:" + "$it")
                        callIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                        var activities = mutableListOf<ResolveInfo>()
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                            activities =  packageManager.queryIntentActivities(
                                callIntent,
                                PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
                            )
                        } else {
                            activities = packageManager.queryIntentActivities(
                                callIntent,
                                PackageManager.GET_META_DATA
                            )
                        }

                        for (j in activities.indices) {
                            if (activities[j].toString().lowercase(Locale.getDefault())
                                    .contains("com.android.phone")
                            ) {
                                callIntent.setPackage("com.android.phone")
                            } else if (activities[j].toString().lowercase(Locale.getDefault())
                                    .contains("call")
                            ) {
                                val pack = activities[j].toString().split("[ ]".toRegex())
                                    .dropLastWhile { it.isEmpty() }
                                    .toTypedArray()[1].split("[/]".toRegex())
                                    .dropLastWhile { it.isEmpty() }.toTypedArray()[0]
                                callIntent.setPackage(pack)
                            }
                        }

                        startActivity(callIntent)

Upvotes: 0

Hamid Zandi
Hamid Zandi

Reputation: 2902

find all app with dial ability

fun getPackagesOfDialerApps(context: Context): List<String> {
    val packageNames = ArrayList<String>()
    // Declare action which target application listen to initiate phone call
    val intent = Intent()
    intent.action = Intent.ACTION_DIAL
    // Query for all those applications
    val resolveInfos = context.packageManager.queryIntentActivities(intent, 0)
    // Read package name of all those applications
    for (resolveInfo in resolveInfos) {
        val activityInfo = resolveInfo.activityInfo
        packageNames.add(activityInfo.applicationInfo.packageName)
    }
    return packageNames
}

call method

val callIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:02188888888"))
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context?.let {
    for(dialerApp in Utils.getPackagesOfDialerApps(it)){
        val appPackageName= dialerApp.toLowerCase(Locale.ENGLISH)
        if(appPackageName == "com.android.phone" ||
            appPackageName  == "com.android.server.telecom" ||
            appPackageName == "com.samsung.android.contacts"){
            callIntent.setPackage(appPackageName)
            break
        }
    }
}
try {
    startActivity(callIntent)
} catch (ex: ActivityNotFoundException){
    callIntent.setPackage(null)
    startActivity(callIntent)
}

Upvotes: 1

COvayurt
COvayurt

Reputation: 895

For a generic use, you can implement like below.

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "1111111111"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


PackageManager packageManager = context.getPackageManager();
List activities = packageManager.queryIntentActivities(callIntent, PackageManager.MATCH_DEFAULT_ONLY);

for(int j = 0 ; j < activities.size() ; j++)
{

    if(activities.get(j).toString().toLowerCase().contains("com.android.phone"))
    {
         callIntent.setPackage("com.android.phone");
    }
    else if(activities.get(j).toString().toLowerCase().contains("call"))
    {
         String pack = (activities.get(j).toString().split("[ ]")[1].split("[/]")[0]);
         callIntent.setPackage(pack);
    }
}

context.startActivity(callIntent);

Also you have to add this intent-filter to activity or receiver etc. in AndroidManifest.xml

<activity>
   <intent-filter>
      <action android:name="android.intent.action.CALL_PRIVILEGED" />
      <data android:scheme="tel" />
   </intent-filter>
</activity>

Finally don't forget to add permission to AndroidManifest.xml

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

Upvotes: 10

Pankaj Kumar
Pankaj Kumar

Reputation: 83028

Use intent.setPackage("com.android.phone");

Like

Intent intent = new Intent(Intent.ACTION_CALL);  
intent.setPackage("com.android.phone");               
intent.setData(Uri.parse("tel:9898989898"));
startActivity(intent);

But better is to let the user to choose.

Read more at How to call from Android Native Dialers, ignore other dialers

Upvotes: 16

Related Questions