1. Get the current program version name
We can set the version number of the program in Androidmanifest.xml, such as android:versionname= "1.0", if you want to get this version number in the code, you can use the following method ( These modifications to the version number only need to modify the settings in the Androidmanifest.xml, without modifying the code):
/*** Returns the current program version name*/ Public Staticstring Getappversionname (Context context) {string Versionname= ""; Try { //---Get the package info---Packagemanager pm =Context.getpackagemanager (); PackageInfo Pi= Pm.getpackageinfo (Context.getpackagename (), 0); Versionname=Pi.versionname; Versioncode=Pi.versioncode; if(Versionname = =NULL|| Versionname.length () <= 0) { return""; } } Catch(Exception e) {LOG.E ("Versioninfo", "Exception", E); } returnVersionname; }
2. Get the version number of the current app
Public Static throws Exception { // Get an instance of Packagemanager packagemanager packagemanager = Getpackagemanager (); // Getpackagename () is the package name of your current class, and 0 means to get version information PackageInfo packinfo = Packagemanager.getpackageinfo (Getpackagename (), 0); = packinfo.versionname; return version; }
3. Get the version number of the current system
/** * Phone system version */public static String getsdkversion () { return Android.os.Build.VERSION.RELEASE; } /** * Mobile System APIlevel */public staticint Getsdkapilevel () { return Android.os.Build.VERSION.SDK_INT; }
Android gets the version number of the current app and the version number of the current system