I'm new to android programming. I've created an app using Eclipse with a MainActivity.java and ClassFragment.java files. ClassFragment is a frangment. And I'm using newly updated appcompat library. I just want to implement a toast in my fragment with will show the version vode and version name of the app. I've used the following code
PackageManager pInfo = getActivity().getPackageManager.getPackageInfo(getActivity().getPackageName, 0);
But its showing java compilation error PackageManager namenotfoundexception. Pls help me out from this. Thanks in advance.
There is one behaviour change in Android API level 30 because of which we get NameNotFoundException when we call getPackageInfo on Android 11 even if the app/package app is installed on user's device. From Android API 30, we need to specify the package name in manifest in order to check if that package is installed on user's device.
<?xml version="1.0" encoding="utf-8"?>
<manifest
...>
<queries>
<package android:name="com.example.Maps" />
</queries>
<application
android:name=".Notes App"
.../>
</manifest>
PackageManager manager = getActivity().getPackageManager();
PackageInfo info;
try {
info = manager.getPackageInfo(getActivity().getPackageName(), 0);
String strVersion = "Version: " + info.versionName;
String strVersionCode = " Build: "+ info.versionCode);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With