如何在本机应用程序而不是浏览器中打开网址?

时间:2017-07-31 08:54:50

标签: android

我发现了一个问题here,但它会检查一个包名。我想要的是检查网址(例如https://www.facebook.comhttps://www.twitter.com)并在其原生应用中打开(如果已安装),或者如果未安装则在浏览器中打开。

String url = "https://www.facebook.com";

if (hasNativeApp()) {
    // open in native app
} else {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
}

2 个答案:

答案 0 :(得分:0)

尝试检查是否安装了应用

Intent linkedinIntent = new Intent(Intent.ACTION_SEND);
linkedinIntent.putExtra(Intent.EXTRA_TEXT, "DATA");
linkedinIntent.setType("text/plain");
boolean linkedinAppFound = false;

List<ResolveInfo> matches2 = this.getPackageManager()
        .queryIntentActivities(linkedinIntent, 0);

for (ResolveInfo info : matches2) {
   if (info.activityInfo.packageName.toLowerCase().startsWith(
            "com.linkedin")) {
        linkedinIntent.setPackage(info.activityInfo.packageName);
        linkedinAppFound = true;
        break;
   }
}
if (linkedinAppFound) {
    Toast.makeText(MainActivity.this, "app found", Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(MainActivity.this, "app not found", Toast.LENGTH_LONG).show();       
}

尝试此操作以获取所有已安装应用程序的列表

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

答案 1 :(得分:0)

您无法从其链接中查看已安装的天气应用程序。包名称是识别App。

所必需的

要检查App Installed是否使用以下功能。

public boolean appInstalledOrNot(Context context, String paramString) {
    PackageManager localPackageManager = context.getPackageManager();
    try {
        //noinspection WrongConstant
        localPackageManager.getPackageInfo(paramString, 1);
        return true;
    } catch (PackageManager.NameNotFoundException ignored) {
    }
    return false;
}

尝试打开原生应用

String facebookPackage = "com.facebook.katana";

if (appInstalledOrNot(this,facebookPackage )) {
    String url = "https://m.facebook.com";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
} else {
    String url = "https://www.facebook.com";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
}

注意:如果您正在寻找facebook和twitter的包名

  

com.facebook.katana - Facebook

     

com.twitter.android - Twtter