使用默认呼叫应用程序在Android应用程序中进

时间:2014-04-22 06:16:05

标签: android android-intent phone-call

我使用以下代码从我的Android应用程序调用:

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

如果Skype已安装在手机中,则会打开Intent Chooser。我想要的是它应该直接从默认调用应用程序调用。

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:14)

使用intent.setPackage("com.android.phone");

喜欢

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

但更好的是让用户选择。

How to call from Android Native Dialers, ignore other dialers

了解详情

答案 1 :(得分:8)

对于通用用途,您可以按照以下方式实施。

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

此外,您必须将此intent-filter添加到AndroidManifest.xml

中的活动或接收器等
<activity>
   <intent-filter>
      <action android:name="android.intent.action.CALL_PRIVILEGED" />
      <data android:scheme="tel" />
   </intent-filter>
</activity>

最后不要忘记向AndroidManifest.xml添加权限

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

答案 2 :(得分:0)

使用拨号功能查找所有应用

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
}

调用方法

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