2016/04/25//////////////////////
by xbw///////////////////////////
Environment Eclipse api22///////////
Make some contribution, send a few small demo, every time you start an app will encounter the app upgrade, this is how to achieve it, first,
There is a picture of the truth,
It's too big to sleep,
Structure.
Config.java
Package Com.example.update;public class Config {public static String mysql_url_update= "http://127.0.0.1/androidapp/ Appupdate/update.txt ";//server-side update URL;}
Fileutil.java
Package Com.example.update;import Java.io.file;import Java.io.ioexception;import android.os.environment;public Class Fileutil {public static file Updatedir = null;public static file Updatefile = null;public static final String applic ation = "Konkaupdateapplication";p ublic static boolean iscreatefilesucess;public static void CreateFile (String app_name ) {if (Android.os.Environment.MEDIA_MOUNTED.equals (Android.os.Environment.getExternalStorageState ())) { iscreatefilesucess = True;updatedir = new File (environment.getexternalstoragedirectory () + "/" +application + "/"); Updatefile = new File (Updatedir + "/" + App_name + ". apk"); if (!updatedir.exists ()) {updatedir.mkdirs ();} if (!updatefile.exists ()) {try {updatefile.createnewfile ();} catch (IOException e) {iscreatefilesucess = false; E.printstacktrace ();}}} Else{iscreatefilesucess = false;}}}
Getversion.java
Package Com.example.update;import Android.content.context;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;public Class getversion {//Gets the version number of the current version public String GetVersion (context context) {try {Packagemanager Packagemanager = Context.getpackagemanager (); PackageInfo PackageInfo = Packagemanager.getpackageinfo (Context.getpackagename (), 0); return packageinfo.versionname ;} catch (Namenotfoundexception e) {e.printstacktrace (); return "version number Unknown";}}}
Isneedupdate.java
Package Com.example.update;import Android.content.context;public class Isneedupdate {private UpdateInfo Info;public Isneedupdate (Context context) {Updateinfoservice Updateinfoservice = new Updateinfoservice (context); try {info = Updateinfoservice.getupdateinfo ();} catch (Exception e) {//TODO auto generate????? E.printstacktrace ();}} public Boolean isneedupdate (context context) {GetVersion version=new getversion ();//version update detection using a floating point type problem, floating point type does not recognize V 1.2.1 This small version//double webversion=double.parsedouble (Info.getversion ());//Double localversion=double.parsedouble ( Version.getversion (context)); Updated if (Info.getversion (). CompareTo (version.getversion (context)) >0) {return true;} else {return) using the Compare dictionary order size detection version false;}} Public String Getdescribe () {return info.getdescription ();} Public String GetUrl () {return Info.geturl ();}}
Showupdatedialog.java
Package Com.example.update;import Android.app.alertdialog;import Android.app.dialog;import android.content.Context ; import Android.content.intent;import Android.os.environment;import android.text.method.ScrollingMovementMethod; Import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imagebutton;import Android.widget.relativelayout;import Android.widget.textview;import Android.widget.toast;public Class Showupdatedialog{private UpdateInfo info;//Update window public Dialog showupdatedialog (final Context context,string msg,final String URL) {layoutinflater layoutinflater =layoutinflater.from (context); Relativelayout layout = (relativelayout) layoutinflater.inflate (r.layout.dialog, NULL); final dialog Dialog = new Alertdialog.builder (context). Create (); Dialog.show (); Dialog.getwindow (). Setcontentview (layout); TextView tex= (TextView) Layout.findviewbyid (R.id.dialog_text); TexTView tex1= (TextView) Layout.findviewbyid (r.id.textview_title); Tex.setmovementmethod (Scrollingmovementmethod.getinstance ()); Tex.settext (msg); Tex1.settext ("Update Prompt"); OK button Btnok = (button) Layout.findviewbyid (R.ID.DIALOG_OK); Btnok.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//services.service (); Intent Intent = new Intent (context,updateservices.class); Intent.putextra ("Key_app_name", Context.getstring (R.string.app_name)); Intent.putextra ("Key_down_url", URL); Context.startservice (intent);} else {toast.maketext (context, "SD card is not available, please insert SD card", Toast.length_short). Show (); Dialog.dismiss (); } }); Close button ImageButton btnclose = (ImageButton) Layout.findviewbyid (r.id.dialog_close); Btnclose.setonclIcklistener (New Onclicklistener () {@Override public void OnClick (View v) {Dia Log.dismiss (); } }); return dialog; }//does not update Windows public Dialog ShowDialog (final context context) {Layoutinflater layoutinflater =layoutinflater.from (Context); Relativelayout layout = (relativelayout) layoutinflater.inflate (r.layout.dialog, NULL); final dialog Dialog = new Alertdialog.builder (context). Create (); Dialog.show (); Dialog.getwindow (). Setcontentview (layout); TextView tex= (TextView) Layout.findviewbyid (R.id.dialog_text); TextView tex1= (TextView) Layout.findviewbyid (r.id.textview_title); GetVersion version=new getversion (); Tex.settext ("You are using the latest version:" +version.getversion (context) + "version"); Tex1.settext ("Update Prompt"); Button Btnok = (button) Layout.findviewbyid (R.ID.DIALOG_OK); Btnok.setvisibility (view.invisible); Close button ImageButton btnclose = (ImageButton) Layout.findviewbyid (r.id.dialog_close); BtnClose.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { Dialog.dismiss (); } }); return dialog;}}
Updateinfo.java
Package Com.example.update;public class updateinfo{ private String version; Private String description; Private String URL; Public String getversion () { return version; } public void Setversion (String version) { this.version = version; } Public String getdescription () { return description; } public void SetDescription (String description) { this.description = description; } Public String GetUrl () { return URL; } public void SetUrl (String url) { this.url = URL; }}
Updateinfoservice.java
Package Com.example.update;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.url;import Android.content.context;public class UpdateInfoService { Public Updateinfoservice (Context context) {}public UpdateInfo getupdateinfo () throws Exception {String path = config.mysq L_url_update; StringBuffer sb = new StringBuffer (); String line = null; BufferedReader reader = null; UpdateInfo UpdateInfo = new UpdateInfo (); try {//create?? URL object url url = new URL (path);//through the URL object, create a HttpURLConnection object (connection) httpurlconnection URLConnection = ( httpurlconnection) url.openconnection ()//Through HttpURLConnection object, get InputStreamReader = new BufferedReader (new InputStreamReader (Urlconnection.getinputstream ()));//Use IO stream to read the text?? while (line = Reader.readline ())! = null) {sb.append (line);}} catch (Exception e) {e.printstacktrace ();} finally {try {if (reader! = null) {Reader.close ();}} catch (Exception e) {E.pri Ntstacktrace ();}} String info = sb.tostring (); updateinfo.setVersion (Info.split ("&") [1]), Updateinfo.setdescription (Info.split ("&") [2]); Updateinfo.seturl (info.split ("&") [3]); return updateInfo;}}
Only to find CSDN blog has a length limit, sent several times have been truncated, so,
Send the demo directly, or go to my little station to see it.
Demo Portal
Station
Small station demo download does not need points OH
Online updates for Android