Android Auto-Update implementation

Source: Internet
Author: User

The main principles are:

When the app launches, go to the server version number, compared to the current application if there is an update to download

Here is the version information for the current app

private void Getcurversion () {        try {            PackageInfo pInfo = Context.getpackagemanager (). Getpackageinfo (                    Context.getpackagename (), 0);            Curversion = Pinfo.versionname;            Curversioncode = Pinfo.versioncode;        } catch (Namenotfoundexception e) {            log.e ("Update", E.getmessage ());            Curversion = "1.0.1";            Curversioncode = 1;        }    }

The following is a comparison of Get version information through the Java NET Package

The server-side format is as follows version_1.0.2

HttpURLConnection gets the input stream, and then uses the

BufferedReader Buffer stream, readline into string, then compare

Private Boolean check_update () {String getstring = null; String version=null;getcurversion (); try {URL myurl=new url (app_check); HttpURLConnection urlconnection= (httpurlconnection) myurl.openconnection (); Urlconnection.setreadtimeout (50000);   Urlconnection.setconnecttimeout (50000); Urlconnection.connect (); InputStream In=urlconnection.getinputstream ();   BufferedReader Buffread;   Buffread=new BufferedReader (New InputStreamReader (in, "Utf-8")); String Line;line=buffread.readline (); while (Line!=null) {getstring+=line;line=buffread.readline ();} int Index=getstring.indexof ("Version_");//2.0.1version=getstring.substring (index+8, index+13); In.close (); LOG.E ("version", version);} catch (Malformedurlexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au    To-generated catch Blocke.printstacktrace ();}    if (version!=null) {if (Version.compareto (curversion) >0) return True;elsereturn false; } else return false;}

The next step is to pop up a dialog box and invoke the download thread

private void Showdowndialog () {alertdialog.builder dialog = new Alertdialog.builder (context);          Dialog.settitle ("software version update");          Dialog.setmessage ("With the latest app updates");         Dialog.setnegativebutton ("Later", New Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {/ /TODO auto-generated method Stubarg0.dismiss ();}              });       Dialog.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { TODO auto-generated method stub//determines the download thread inside the call, while displaying the download progress dialog Dialog.dismiss (); cancel=true;downapk (); showdownapk ();}              } );       Dialog.show ();}

The last one is to issue a intent broadcast

private void Setinstall () {
file Apkfile = new File (apk_path);  
Span style= "White-space:pre" >        if (!apkfile.exists ()) { 
           return; &NBSP
       }      
       intent i = new Intent (Intent.action_view); &NBSP
       i.setdataandtype (Uri.parse ("file://" + Apkfile.tostring ()), "application/vnd.android.package-archive");  &NBSP
       context.startactivity (i); 
}

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.