Public classMainactivityextendsactionbaractivity {PrivateTextView Mtextview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Gets the current project version numberMtextview =(TextView) Findviewbyid (R.ID.TEXTVIEW1); Try{getversionname (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //Android System version informationString model_version= "Android system version info:" + Android.os.Build.MODEL + ", API" + Android.os.Build.VERSION.SDK + ", an Droid "+Android.os.Build.VERSION.RELEASE; //Display Information Try{Mtextview.settext ("Current Program version:" + getversionname () + "\ n" +model_version); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } PrivateString Getversionname ()throwsException {//get an instance of PackagemanagerPackagemanager Packagemanager =Getpackagemanager (); //Getpackagename () is the package name of your current class, and 0 means to get version informationPackageInfo packinfo = Packagemanager.getpackageinfo (Getpackagename (), 0); String version=Packinfo.versionname; returnversion; }
}
Get Android version information and program version number