1

I found a question here, but it will check a package name. What I want is to check a url (e.g. https://www.facebook.com or https://www.twitter.com) and open in in their native app if it's installed, or open it in the browser if it's not installed.

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);
}
Sam
  • 351
  • 1
  • 2
  • 15

2 Answers2

0

try this to check app is installed or not

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

try this to get a list of all installed applications

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • By doing this, we need to know the package name beforehand, right? What happened if I can't know the link and also the package name beforehand? – Sam Jul 31 '17 at 09:18
0

You can't check weather app installed or not from it's link. Package name is required for identifying App.

To check App Installed or not use following Function.

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

Try this for Opening native App

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

NOTE : if you are looking for package name of facebook and twitter

com.facebook.katana - Facebook

com.twitter.android - Twtter

Community
  • 1
  • 1
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • what happened if the url link cannot be known beforehand? It's impossible to maintain the database of all apps to check if native app is available or not. So, there's no way to open a link in the native app by default if it's available and open in it the browser if it's not available? – Sam Jul 31 '17 at 09:17
  • in My knowledge.. NO.. it is not possible. App can be identified with it's package name. – V-rund Puro-hit Jul 31 '17 at 09:42
  • as long as you are worrying about package name.. you can always get it from play store link of particular application. – V-rund Puro-hit Jul 31 '17 at 09:44