I’m experiencing an issue with the getPackageSource() method in my Android application. Despite installing the application via the Google Play Store, getPackageSource() always returns 0, which indicates that the package source is unknown.
Here is the code snippet I’m using to check the package source:
PackageManager packageManager = webView.getContext().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(webView.getContext().getPackageName(), 0);
if (packageInfo != null && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
InstallSourceInfo sessionInfo = packageManager.getInstallSourceInfo(webView.getContext().getPackageName());
if (sessionInfo != null) {
int source = sessionInfo.getPackageSource();
switch (source) {
case PackageInstaller.PACKAGE_SOURCE_STORE:
Log.d("Installed from store", "true");
break;
case PackageInstaller.PACKAGE_SOURCE_DOWNLOADED_FILE:
case PackageInstaller.PACKAGE_SOURCE_LOCAL_FILE:
case PackageInstaller.PACKAGE_SOURCE_OTHER:
case PackageInstaller.PACKAGE_SOURCE_UNSPECIFIED:
showPopup();
break;
}
}
}
I was expecting the getPackageSource() method to return as a 2 when the application is installed via the Google Play Store (match with PackageInstaller.PACKAGE_SOURCE_STORE).
Rayiisp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.