Android APK version update

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:32px; color:#FF0000">Android APK version update</span></p></p><p>/**<br>* Get version Name<br>*<br>* @return Version<br>*/<br>Private String getversionname () {<br>try {<br>Get an instance of Packagemanager<br>Packagemanager Packagemanager = Getpackagemanager ();<br>Getpackagename () is the package name of your current class, and 0 means to get version information<br>PackageInfo Packinfo = Packagemanager.getpackageinfo (<br>Getpackagename (), 0);<br>Version = Packinfo.versionname;//versionname is to get the release name and version number, etc...<br>} catch (namenotfoundexception E) {<br>E.printstacktrace ();<br>}<br>Return version;<br>}</p><p><p>Activity class Invocation</p></p><p><p>Here to check if the version needs to be updated<br>Updatemanager Mupdatemanager = new Updatemanager (<br>settingactivity.this);<br>Mupdatemanager.checkupdateinfo ();</p></p><p>public class Updatemanager {<br><br>Private Context mcontext;<br>/* Download Package Installation path */<br>private static final String Savepath = "//sdcard//updatedemo//";<br>private static final String Savefilename = Savepath + "licaike.apk";<br>/* progress bar with notification UI refreshed handler and MSG constants */<br><br>private int progress;<br>Private Progressdialogutil pdu;<br><br>Private Boolean Interceptflag = true;<br><br>@SuppressLint ("handlerleak")<br>Private Handler Mhandler = new Handler () {<br>public void Handlemessage (Message Msg) {<br>Switch (msg.what) {<br>Case 0x123:<br>Pdu.setprogress (progress);<br>Break<br>Case 0x124://install APK<br>File Apkfile = new file (savefilename);<br>If (!apkfile.exists ()) {<br>Return<br>}<br>Intent i = new Intent (intent.action_view);<br>I.setdataandtype (uri.parse ("file://" + apkfile.tostring ()),<br>"application/vnd.android.package-archive");<br>Mcontext.startactivity (i);<br>Pdu.dismiss ();<br>Break<br>Default<br>Break<br>}<br>};<br>};<br><br>Public Updatemanager (context Context) {<br>This.mcontext = context;<br>}<br><br>The external interface lets the main activity call<br>public void Checkupdateinfo () {<br>Final Alertdialogutil dialogutil = new Alertdialogutil (mcontext, false,<br>null);<br>Dialogutil.setmessage ("please Update the new version");<br>Dialogutil.setbtnpositivevalue ("update");<br>Dialogutil.setpositiveclicklistener (new View.onclicklistener () {<br><br>@Override<br>public void OnClick (View Arg0) {<br>Showdownloaddialog ();<br>Dialogutil.dismiss ();<br>}<br>});<br>Dialogutil.setbtnnegativevalue ("cancel");<br>Dialogutil.setnegativeclicklistener (new View.onclicklistener () {<br><br>@Override<br>public void OnClick (View Arg0) {<br>Dialogutil.dismiss ();<br>}<br>});<br>Dialogutil.show ();<br><br>}<br><br>private void Showdownloaddialog () {<br>Download apk<br>Downloadthread ();<br>PDU = new Progressdialogutil (mcontext, false, null);<br>Pdu.setmessage ("software is being updated ...");<br>Pdu.setbtnnegativevalue ("cancel");<br>Pdu.setnegativeclicklistener (new View.onclicklistener () {<br><br>@Override<br>public void OnClick (View Arg0) {<br>Interceptflag = false;<br>Pdu.dismiss ();<br>}<br>});<br>Pdu.show ();<br>}<br><br>private void Downloadthread () {<br>New Thread (new Runnable () {<br><br>@Override<br>public void Run () {<br>InputStream is = null;<br>FileOutputStream fos = null;<br>try {<br>Return the installation package URL<br>String Apkurl = "http://d.m.hexun.com/app/licaike.apk";<br>URL url = new URL (apkurl);<br>HttpURLConnection conn = (httpurlconnection) URL<br>. OpenConnection ();<br>Conn.connect ();<br>int length = Conn.getcontentlength ();<br>is = Conn.getinputstream ();<br><br>File File = new file (savepath);<br>If (!file.exists ()) {<br>File.mkdir ();<br>}<br>String Apkfile = savefilename;<br>File Apkfile = new file (apkfile);<br>FOS = new FileOutputStream (apkfile);<br><br>int count = 0;<br>byte buf[] = new byte[1024];<br>While (interceptflag) {//click Cancel to stop the Download.<br>int numread = Is.read (buf);<br>Count + = numread;<br>progress = (int) ((float) Count/length) * 100);<br>Update progress<br>Mhandler.sendemptymessage (0x123);<br>If (numread <= 0) {<br>Download Complete Notification Installation<br>Mhandler.sendemptymessage (0x124);<br>Break<br>}<br>Fos.write (buf, 0, numread);<br>}<br>} catch (Exception E) {<br>E.printstacktrace ();<br>} finally {<br>try {<br>If (fos! = Null)<br>Fos.close ();<br>If (is = Null)<br>Is.close ();<br>} catch (Exception E) {<br>} finally {<br>FOS = null;<br>is = null;<br>}<br>}<br><br>}<br>}). Start ();<br>}<br><br>}</p><p><p><span style="color:#FF0000">Alertdialog Tool class My blog has been written, you can see yourself write, you can paste it</span></p></p><p><p>http://blog.csdn.net/menglele1314/article/details/45718501<br></p></p><p><p>The progress bar you go to the dialog box to drag a<br></p></p><p><p><br></p></p><p><p>Android APK version update</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.