Android-get app version name and version number
Last Update:2016-03-23
Source: Internet
Author: User
<span id="Label3"></p><pre><pre>Using the Package Manager<br><br></pre></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.liunan.myfirstapp.util;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.Context;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.pm.ApplicationInfo;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.pm.PackageInfo;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.pm.PackageManager;</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Created by Liu Nan on 2016-03-22.</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Packageutils {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Get version name *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Context Contexts *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">Version name</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">String getversionname (context Context) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Package Manager</span></span>Packagemanager pm =<span style="color: #000000;"><span style="color: #000000;">Context.getpackagemanager (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get package Information</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{packageinfo PackageInfo</span></span>= Pm.getpackageinfo (context.getpackagename (), 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return version number</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">packageinfo.versionname; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(packagemanager.namenotfoundexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Get version number *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Context Contexts *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">Version number</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Getversioncode (context Context) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Package Manager</span></span>Packagemanager pm =<span style="color: #000000;"><span style="color: #000000;">Context.getpackagemanager (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get package Information</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{packageinfo PackageInfo</span></span>= Pm.getpackageinfo (context.getpackagename (), 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return version number</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">packageinfo.versioncode; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(packagemanager.namenotfoundexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>0<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Get the name of the app *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Context Contexts *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">name</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">String getappname (context Context) {packagemanager pm</span></span>=<span style="color: #000000;"><span style="color: #000000;">Context.getpackagemanager (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get package Information</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{packageinfo PackageInfo</span></span>= Pm.getpackageinfo (context.getpackagename (), 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get app Info</span></span>ApplicationInfo ApplicationInfo =<span style="color: #000000;"><span style="color: #000000;">packageinfo.applicationinfo; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Albelres</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Labelres =<span style="color: #000000;"><span style="color: #000000;">applicationinfo.labelres; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">returns the name of the app</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">context.getresources (). getString (labelres); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(packagemanager.namenotfoundexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; }}</span></span></pre><p><p></p></p><p><p>Android-get app version name and version number</p></p></span>