问题
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