1, get the application name;
2. Get Application version information.
1 ImportAndroid.content.Context;2 ImportAndroid.content.pm.PackageInfo;3 ImportAndroid.content.pm.PackageManager;4 Importandroid.content.pm.PackageManager.NameNotFoundException;5 6 //helper classes related to apps7 Public classapputils8 {9 Ten Privateapputils () One { A /*cannot be instantiated*/ - Throw NewUnsupportedoperationexception ("cannot be instantiated"); - the } - - /** - * Get application name + */ - Public StaticString Getappname (context context) + { A Try at { -Packagemanager Packagemanager =Context.getpackagemanager (); -PackageInfo PackageInfo =Packagemanager.getpackageinfo ( -Context.getpackagename (), 0); - intLabelres =packageInfo.applicationInfo.labelRes; - returncontext.getresources (). getString (labelres); in}Catch(namenotfoundexception e) - { to e.printstacktrace (); + } - return NULL; the } * $ /**Panax Notoginseng * [Get Application version name information] - * the * @paramContext + * @returnThe version name of the current app A */ the Public StaticString Getversionname (context context) + { - Try $ { $Packagemanager Packagemanager =Context.getpackagemanager (); -PackageInfo PackageInfo =Packagemanager.getpackageinfo ( -Context.getpackagename (), 0); the returnPackageinfo.versionname; - Wuyi}Catch(namenotfoundexception e) the { - e.printstacktrace (); Wu } - return NULL; About } $ -}
Android development tool Class 01_apputils