Detecting programmatically whether an app is installed on Android

本秂侑毒 提交于 2019-12-11 02:08:14

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/12273907/detecting-programmatically-whether-an-app-is-installed-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!