Android version automatic detection update Version Detection automatic update Automatic Installation

Source: Internet
Author: User

Import Java. io. file; </P> <p> Import Java. io. fileoutputstream; </P> <p> Import Java. io. ioexception; </P> <p> Import Java. io. inputstream; </P> <p> Import Org. apache. HTTP. httpentity; <br/> Import Org. apache. HTTP. httpresponse; <br/> Import Org. apache. HTTP. client. clientprotocolexception; <br/> Import Org. apache. HTTP. client. httpclient; <br/> Import Org. apache. HTTP. client. methods. httpget; <br/> Import Org. apache. HTTP. Impl. client. defaulthttpclient; </P> <p> Import android. app. alertdialog; <br/> Import android. app. dialog; <br/> Import android. app. progressdialog; <br/> Import android. content. dialoginterface; <br/> Import android. content. intent; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. OS. handler; </P> <p> public class update extends activity {<br /> Public progressdialog pbar; <br/> private handler = new handler (); </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. update); <br/> dialog = new alertdialog. builder (update. this ). settitle ("system update") <br/>. setmessage ("new version found. Please update it! ") // Set content <br/>. setpositivebutton ("OK", // set the OK button <br/> New dialoginterface. onclicklistener () {</P> <p> @ override <br/> Public void onclick (dialoginterface dialog, <br/> int which) {<br/> pbar = new progressdialog (update. this); <br/> pbar. settitle ("downloading"); <br/> pbar. setmessage ("Please wait... "); <br/> pbar <br/>. setprogressstyle (progressdialog. style_spinner); <br/> downfile ("http: // URL: 8765/oa.apk"); </ P> <p >}</P> <p> }). setnegativebutton ("cancel", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, <br/> int whichbutton) {<br/> // click "cancel" to exit the Program </P> <p >}< br/> }). create (); // create <br/> // display dialog box <br/> dialog. show (); </P> <p >}</P> <p> void downfile (final string URL) {<br/> pbar. show (); <br/> New thread () {<br/> Public void run () {<br/> httpclient Client = New defaulthttpclient (); <br/> // Params [0] indicates the connected URL <br/> httpget get = new httpget (URL); <br/> httpresponse response; <br/> try {<br/> response = client.exe cute (get); <br/> httpentity entity = response. getentity (); <br/> long length = entity. getcontentlength (); <br/> inputstream is = entity. getcontent (); <br/> fileoutputstream = NULL; <br/> If (is! = NULL) {</P> <p> file = new file (Environment <br/>. getexternalstoragedirectory (), "oa.apk"); <br/> fileoutputstream = new fileoutputstream (File); </P> <p> byte [] Buf = new byte [1024]; <br/> int CH =-1; <br/> int COUNT = 0; <br/> while (CH = is. read (BUF ))! =-1) {<br/> // baos. write (BUF, 0, CH); <br/> fileoutputstream. write (BUF, 0, CH); <br/> count + = CH; <br/> If (length> 0) {</P> <p >}< br/> fileoutputstream. flush (); <br/> If (fileoutputstream! = NULL) {<br/> fileoutputstream. close (); <br/>}< br/> down (); <br/>} catch (clientprotocolexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p> }. start (); </P> <p >}</P> <p> void down () {<br/> handler. post (New runnable () {<br/> Public void run () {<br/> pbar. cancel (); <br/> Update (); <br/>}< br/>}); <br/>}</P> <p> void Update () {</P> <p> intent = new intent (intent. action_view); <br/> intent. setdataandtype (URI. fromfile (new file ("/sdcard/oa.apk"), <br/> "application/vnd. android. package-Archive "); <br/> startactivity (intent); <br/>}< br/>

 

 

An android tool class used in the past, Version Detection and automatic update. Poor versatility, right to take notes.

 

Package COM. hiyo. game. PDK. tool; <br/> Import Java. io. file; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. inputstream; <br/> Import java.net. URL; <br/> Import java.net. urlconnection; <br/> Import android. app. activity; <br/> Import android. app. alertdialog; <br/> Import android. app. progressdialog; <br/> Import android. content. context; <br/> Import android. content. dialoginterface; <br/> I Mport android. content. intent; <br/> Import android. content. PM. packageinfo; <br/> Import android. content. PM. packagemanager. namenotfoundexception; <br/> Import android.net. connectivitymanager; <br/> Import android.net. networkinfo; <br/> Import android.net. uri; <br/> Import android. util. log; <br/> Import android. webKit. urlutil; <br/> Import COM. hiyo. game. PDK. activity. r; <br/>/** <br/> * Android auto Update. <br/> * lazybone/2010.08.20 <br/> * 1.set apkurl. <br/> * 2. check (). <br/> * 3.add delfile () method in resume ()/onpause (). <br/> */<br/> public class myautoupdate {<br/> public activity = NULL; <br/> Public int versioncode = 0; <br/> Public String versionname = ""; <br/> Private Static final string tag = "AutoUpdate"; <br/> private string currentf Ilepath = ""; <br/> private string currenttempfilepath = ""; <br/> private string fileex = ""; <br/> private string filena = ""; <br/> private string strurl = "http: // 127.0.0.1: 81/apidemos.apk"; <br/> private progressdialog dialog; <br/> Public myautoupdate (activity) {<br/> This. activity = activity; <br/> getcurrentversion (); <br/>}< br/> Public void check () {<br/> If (isnetworkavailab Le (this. activity) = false) {<br/> return; <br/>}< br/> If (true) {// check version. <br/> showupdatedialog (); <br/>}< br/> Public static Boolean isnetworkavailable (context CTX) {<br/> try {<br/> connectivitymanager CM = (connectivitymanager) CTX <br/>. getsystemservice (context. connectivity_service); <br/> networkinfo info = cm. getactivenetworkinfo (); <br/> return (info! = NULL & info. isconnected (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/> return false; <br/>}< br/> Public void showupdatedialog () {<br/> @ suppresswarnings ("UNUSED") <br/> alertdialog Alert = new alertdialog. builder (this. activity) <br/>. settitle ("title") <br/>. seticon (R. drawable. icon) <br/>. setmessage ("Update or not? ") <Br/>. setpositivebutton ("Update", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, <br/> int which) {<br/> downloadthefile (strurl); <br/> showwaitdialog (); <br/>}< br/>}) <br/>. setnegativebutton ("cancel", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, <br/> int which) {<br/> dialog. cancel (); <br />}< Br/> }). show (); <br/>}< br/> Public void showwaitdialog () {<br/> dialog = new progressdialog (activity); <br/> dialog. setmessage ("waitting for update... "); <br/> dialog. setindeterminate (true); <br/> dialog. setcancelable (true); <br/> dialog. show (); <br/>}< br/> Public void getcurrentversion () {<br/> try {<br/> packageinfo = activity. getpackagemanager (). getpackageinfo (<br/> activity. Getpackagename (), 0); <br/> This. versioncode = info. versioncode; <br/> This. versionname = info. versionname; <br/>}catch (namenotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/> private void downloadthefile (final string strpath) {<br/> fileex = strurl. substring (strurl. lastindexof (". ") + 1, strurl. length () <br/>. tolowercase (); <br/> filena = strurl. substring (strurl. lastindex Of ("/") + 1, <br/> strurl. lastindexof (". "); <br/> try {<br/> If (strpath. equals (currentfilepath) {<br/> dodownloadthefile (strpath); <br/>}< br/> currentfilepath = strpath; <br/> runnable r = new runnable () {<br/> Public void run () {<br/> try {<br/> dodownloadthefile (strpath); <br/>} catch (exception E) {<br/> log. E (TAG, E. getmessage (), e); <br/>}< br/>}; <br/> New thread (R ). sta RT (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> private void dodownloadthefile (string strpath) throws exception {<br/> log. I (TAG, "getdatasource ()"); <br/> If (! Urlutil. isnetworkurl (strpath) {<br/> log. I (TAG, "getdatasource () It's a wrong URL! "); <Br/>}else {<br/> URL myurl = new URL (strpath); <br/> urlconnection conn = myurl. openconnection (); <br/> Conn. connect (); <br/> inputstream is = Conn. getinputstream (); <br/> If (is = NULL) {<br/> throw new runtimeexception ("stream is null "); <br/>}< br/> file mytempfile = file. createtempfile (filena ,". "+ fileex); <br/> currenttempfilepath = mytempfile. getabsolutepath (); <br/> fileoutputstream Fos = new fileoutputstream (mytempfile); <br/> byte Buf [] = new byte [128]; <br/> do {<br/> int numread = is. read (BUF); <br/> If (numread <= 0) {<br/> break; <br/>}< br/> FOS. write (BUF, 0, numread); <br/>}while (true); <br/> log. I (TAG, "getdatasource () download OK... "); <br/> dialog. cancel (); <br/> dialog. dismiss (); <br/> openfile (mytempfile); <br/> try {<br/> is. close (); <br/>}catch (exception ex) {<br/> log. E (TAG, "getdatasource () error:" + ex. getmessage (), Ex); <br/>}< br/> private void openfile (file F) {<br/> intent = new intent (); <br/> intent. addflags (intent. flag_activity_new_task); <br/> intent. setaction (Android. content. intent. action_view); <br/> string type = getmimetype (f); <br/> intent. setdataandtype (URI. fromfile (F), type); <br/> activity. startactivity (intent); <br/>}< br/> Public void delfile () {<br/> log. I (TAG, "The tempfile (" + currenttempfilepath + ") was deleted. "); <br/> file myfile = new file (currenttempfilepath); <br/> If (myfile. exists () {<br/> myfile. delete (); <br/>}< br/> private string getmimetype (file F) {<br/> string type = ""; <br/> string fname = f. getname (); <br/> string end = fname <br/>. substring (fname. lastindexof (". ") + 1, fname. length () <br/>. tolowercase (); <br/> If (end. equals ("m4a") | end. equals ("MP3") | end. equals ("mid") <br/> | end. equals ("xmf") | end. equals ("Ogg") | end. equals ("WAV") {<br/> type = "audio"; <br/>}else if (end. equals ("3GP") | end. equals ("MP4") {<br/> type = "video"; <br/>}else if (end. equals ("jpg") | end. equals ("GIF") | end. equals ("PNG") <br/> | end. equals ("Jpeg") | end. equals ("BMP") {<br/> type = "image"; <br/>}else if (end. equals ("APK") {<br/> type = "application/vnd. android. package-Archive "; <br/>}else {<br/> type =" * "; <br/>}< br/> If (end. equals ("APK") {<br/>} else {<br/> type + = "/*"; <br/>}< br/> return type; <br/>}< br/>}

 

// Obtain the package name and version: </P> <p> packagemanager manager = activity. getpackagemanager (); </P> <p> packageinfo info = manager. getpackageinfo (activity. getpackagename (), 0); </P> <p> packagename = info. packagename; </P> <p> versionname = info. versionname; <br/> // obtain the server version comparison here </P> <p> // download the APK to the local package_save_path </P> <p> // install the application <br/> intent = new intent (intent. action_view); </P> <p> intent. addflags (intent. flag_activity_new_task); </P> <p> intent. setdataandtype (URI. parse ("file: //" + package_save_path), </P> <p> "application/vnd. android. package-Archive "); </P> <p> activity. startactivity (intent );

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.