如何知道Android中是否已成功安装应用程序

时间:2011-04-11 10:39:24

标签: android uri android-install-apk

如何知道Android中是否已成功安装应用?我使用以下方法安装apk文件。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);

2 个答案:

答案 0 :(得分:3)

您可以查询已安装软件包的列表并查找刚刚安装的软件包:

List pkgAppsList = context.getPackageManager().getInstalledPackages();

http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledPackages%28int%29

答案 1 :(得分:3)

private boolean isAppInstalled(String uri) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}

只需传递您需要检查的应用程序的包名称即可调用该方法。

if(isAppInstalled("com.yourpackage.package")){
//app installed
}
else{
//app not installed
}