以编程方式从Android App启动Skype

时间:2011-06-19 22:17:36

标签: java android android-intent android-activity skype

我使用默认的os拨号程序直接创建一个调用:

Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:" + phoneNo));
startActivity(call);

是否可以直接从我的应用启动Skype?

我尝试按如下方式传递数字:

PackageManager packageManager = getPackageManager();
Intent skype = packageManager.getLaunchIntentForPackage("com.skype.raider");
skype.setData(Uri.parse("tel:65465446"));
startActivity(skype);

传递号码失败。

2 个答案:

答案 0 :(得分:3)

您需要知道Skype软件包名称(例如:com.skype.android),然后您可以启动它:

PackageManager packageManager = getPackageManager();
startActivity(packageManager.getLaunchIntentForPackage("com.skype.android"));

答案 1 :(得分:0)

在您的情况下,可能会发生以下情况:

  1. 未安装Skype
  2. Skype已禁用
  3. 已安装Skype

对于情况1和2,您将无法呼叫Skype。对于情况3,您可以通过Skype致电。请检查以下情况以启动Skype:

String appName = "Skype";
String packageName = "com.skype.raider";
openApp(context, appName, packageName);

public static void openApp(Context context, String appName, String packageName) {
    if (isAppInstalled(context, packageName))
        if (isAppEnabled(context, packageName))
            context.startActivity(context.getPackageManager().getLaunchIntentForPackage(packageName));
        else Toast.makeText(context, appName + " app is not enabled.", Toast.LENGTH_SHORT).show();
    else Toast.makeText(context, appName + " app is not installed.", Toast.LENGTH_SHORT).show();
}

检查是否已安装应用程序:

private static boolean isAppInstalled(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return false;
    }

检查是否已启用应用程序:

private static boolean isAppEnabled(Context context, String packageName) {
        boolean appStatus = false;
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName, 0);
            if (ai != null) {
                appStatus = ai.enabled;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appStatus;
    }