Android version detection \ automatic update (to others)

Source: Internet
Author: User

Android version detection \ automatic update

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

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.