How to know if an app has been installed successfully in android

我是研究僧i 提交于 2019-12-13 15:18:52

问题


How can I know if an app has been installed successfully in android? I am using the following method to install apk files.

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

回答1:


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

Just call the method by passing the package name of the application you need to check.

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



回答2:


You can query the list of installed packages and look for the one you just installed:

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

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



来源:https://stackoverflow.com/questions/5620112/how-to-know-if-an-app-has-been-installed-successfully-in-android

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