6

I have registered a url scheme for my Android app (let's say myapp://host).

On my other app, I can launch that app by using Intent, but how do I check whether the first app is installed without launching it?

in iOS, it is as easy as

[[UIApplication sharedApplication] canOpenUrl:@"myapp://"];

is there an equivalent in Android? (easy way)

I'd like to check using url scheme. I know how to check using package name, but I do not know how to get the url scheme from PackageInfo as well... (harder way)

    PackageManager pm = m_cContext.getPackageManager();
    boolean installed = false;
    try {
        @SuppressWarnings("unused")
        PackageInfo pInfo = pm.getPackageInfo(szPackageName, PackageManager.GET_ACTIVITIES);
        installed = true;
    } 
    catch (PackageManager.NameNotFoundException e) {
        installed = false;
    }

Thanks in advance!

Note: This is the Android version of the same question for iOS: Detecting programmatically whether an app is installed on iPhone

Community
  • 1
  • 1
Zennichimaro
  • 5,236
  • 6
  • 54
  • 78

2 Answers2

3

If you know how to launch the app, then create the intent that launches your app and then call queryIntentActivities

 Intent intent = //your app launching intent goes here
 PackageManager packageManager = mContext.getPackageManager();
 List<ResolveInfo> resolvedActivities = packageManager.queryIntentActivities(intent,0);
 if(resolvedActivities.size() >0)
     \\present
Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • 3
    This will generally work, however there might be multiple activities that can handle the URL, so it is better to actually check the package name (`ResolveInfo.activityInfo.packageName`). It is further complicated by some changes vendors have been making to avoid (as usual, questionable) Applet patents. Some more details here: http://commonsware.com/blog/2012/07/24/linkify-problem-detection-mitigation.html – Nikolay Elenkov Sep 05 '12 at 03:17
1

I use queryIntentActivities from class PackageManager in a static method :

public static boolean canOpenIntent(Context context, Intent intent)
{
    boolean canOpenUrl = false;
    PackageManager    packageManager     = context.getPackageManager();
    List<ResolveInfo> resolvedActivities = packageManager.queryIntentActivities(intent, 0);
    if(resolvedActivities.size() > 0)
        canOpenUrl = true;
    return canOpenUrl;
}
Vincent Ducastel
  • 10,121
  • 2
  • 17
  • 11
  • Since Android 11 the OS severely limits the list of apps discoverable with this method (or PackageManager in general). If you want to check a third-party app intent, you need to add a `` entry like this: ``. Source: https://developer.android.com/training/package-visibility – javaxian Nov 02 '22 at 08:26