Make Call in Android Application using default call application

前端 未结 3 1670
无人及你
无人及你 2020-12-11 03:32

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

Intent intent = new Intent(Intent.ACTION_CALL);                 
intent.setData(Uri.par         


        
相关标签:
3条回答
  • 2020-12-11 04:06

    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

    0 讨论(0)
  • 2020-12-11 04:11

    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" />
    
    0 讨论(0)
  • 2020-12-11 04:16

    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)
    }
    
    0 讨论(0)
提交回复
热议问题