4

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
  • 4,986
  • 5
  • 48
  • 76

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,202
  • 8
  • 49
  • 83
nandeesh
  • 24,530
  • 6
  • 66
  • 77
  • 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
0

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,061
  • 2
  • 16
  • 11