Android Updataapp Update installation method encapsulation,

Source: Internet
Author: User
Tags sendmsg

I've been busy looking for work for a while, and there's no time to update the blog. I have already gone to work in the new company. Nothing on the first update, this period of time feel that they still have a lot of things to learn, every interview feel that they can know a lot of things, such as the mainstream of what, we all use what things, are still quite good, but do not like to change jobs. More irritable. Okay, here's the complaint. Let's get to the chase today,

On the code:


/* @author X.J update installation APK * @time 2014.09.18 */public class Uploadapp {private ProgressDialog mpdialog;//create precision bar private int fi lesize;//set File size private int downloadfilesize;//The size of the currently downloaded file private Context mcontext;//apk installation path private static final String Savepath = "/sdcard/updatedemo/"; Save the path to the downloaded file private static final String Savefilename = Savepath + "updatedemo.apk";//Save the download file name/** * Prompt user to update * * @param Mcontext * @param url. Download link * @param str. Update content */public void Uploadapp (Context mcontext, String str, final String URL) {This . Mcontext = Mcontext; Alertdialog.builder builder = new Builder (mcontext); Builder.setmessage ("There is a new version upgrade, do you want to download the installation?") \ n "+ str"), Builder.settitle ("System Version Update");//str can prompt for content display Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, int which) {mpdialog = new ProgressDialog (Mcontext); Mpdialog.setprogressstyle (progressdialog.style_horizontal); Mpdialog.settitle ("hint"); Mpdialog.setmessage ("In Download, please later"); Mpdialog.setindeterminate (false);Is whether the progress bar is clear mpdialog.setcancelable (false);//Click Back button to cancel the dialog box Mpdialog.setcanceledontouchoutside (FALSE);// Click the External Cancel dialog box to display mpdialog.setprogress (0);//Set the initial progress bar to 0mpdialog.incrementprogressby (1);//Set the progress bar to rise. Mpdialog.show (); new Thread () {public void run () {String apkurl = url;//download apk urlurl url = null;try {url = new URL (apkurl) ; HttpURLConnection con = (httpurlconnection) url.openconnection (); InputStream in = Con.getinputstream (); fileSize = Con.getcontentlength ();//Gets the length of the downloaded file = the filename File = new file (Savepath); if (!file.exists ()) {File.mkdir (); File Fileout = new file (savefilename);//download file's storage address fileoutputstream out = new FileOutputStream (fileout); byte[] bytes = new BYTE[1024];d ownloadfilesize = 0;sendmsg (0);//Sendmeg is 0 when the download is complete int c;while ((c = in.read (bytes))! =-1) {Out.write ( Bytes, 0, c);d ownloadfilesize + = c;sendmsg (1);} In.close (); Out.close ();}} catch (Exception e) {e.printstacktrace ();} Sendmsg (2);}}. Start ();d Ialog.dismiss ();}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show (); Install the APK method private void installapk (String filename) {File File = new file (filename); Intent Intent = new Intent (); Intent.add Flags (Intent.flag_activity_new_task); intent.setaction (Intent.action_view); String type = "Application/vnd.android.package-archive"; Intent.setdataandtype (uri.fromfile (file), type); Mcontext.startactivity (intent); if (mpdialog! = null) {Mpdialog.cancel ();}} private void sendmsg (int flag) {Message msg = new Message (); msg.what = Flag;handler.sendmessage (msg);} Private final Handler Handler = new Handler (Looper.getmainlooper ()) {public void Handlemessage (Message msg) {if (! Thread.CurrentThread (). isinterrupted ()) {switch (msg.what) {case 0:mpdialog.setmax (+); Break;case 1:int result = Downloadfilesize * 100/filesize;mpdialog.setprogress (Result), Break;case 2:mpdialog.setmessage ("File download Complete"); INSTALLAPK (savefilename); break;case-1:string error = Msg.getdata (). getString ("error"); Mpdialog.setmessage (error); break;default:break;}} Super.handlemessage (msg);}};}



There may be a need to add network permissions to the permissions, as it is necessary to download the APK from the network and then overwrite the installation. And then this place could have an exception where the current directory is not found. You also need to add permissions at this time to allow write permissions.

<!--permission to write data to SDcard--><uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission><!--permissions to create/delete files in SDcard--><uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "></uses-permission>

Android Updataapp Update installation method encapsulation,

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.