Download with xutils3.0 and xutils3.0
The write example is relatively simple. xutils3.0 is used to download project updates.
1. First, use a network request to determine whether the version is to be updated.
2. If you want to update the file, a pop-up window will pop up. I use the system's built-in Dialog to display the downloaded version number and the downloaded content prompt.
3. When the user clicks download, the download starts. A horizontal progress bar is displayed during the download.
4. After the download is complete, the progress bar disappears and the system is called to install apk
The xutils jar package used by the Code is 3.3.32.
1 package com. item. weixin; 2 3 import java. io. file; 4 5 import org. json. JSONException; 6 import org. json. JSONObject; 7 import org. xutils. x; 8 import org. xutils. common. callback; 9 import org. xutils. http. requestParams; 10 11 import android. annotation. suppressLint; 12 import android. app. activity; 13 import android. app. alertDialog; 14 import android. app. progressDialog; 15 import android. content. context; 16 import android. content. dialogInterface; 17 import android. content. intent; 18 import android. content. pm. packageInfo; 19 import android. content. pm. packageManager. nameNotFoundException; 20 import android.net. uri; 21 import android. OS. bundle; 22 import android. OS. environment; 23 import android. view. view; 24 import android. view. window; 25 import android. widget. button; 26 27/** 28 * Set xutils to download 29*30 * @ author Administrator 31*32 */33 public class LoginActivity extends Activity {34 private Button ll_update; 35 private ProgressDialog pDialog; 36 private String nowVersion; 37 private ProgressDialog ssdialog; 38 39 @ Override 40 protected void onCreate (Bundle savedInstanceState) {41 // TODO Auto-generated method stub 42 super. onCreate (savedInstanceState); 43 requestWindowFeature (Wi Ndow. FEATURE_NO_TITLE); 44 setContentView (R. layout. activity_login); 45 ll_update = (Button) findViewById (R. id. btn_login); 46 ll_update.setOnClickListener (new View. onClickListener () {47 48 @ Override 49 public void onClick (View arg0) {50 // TODO Auto-generated method stub 51 checkUpdate (); 52} 53 }); 54 try {55 PackageInfo packageInfo = getPackageManager (). getPackageInfo (56 getPackageName (), 0); 57 nowVersion = packageInfo. versionName; 58} catch (NameNotFoundException e) {59 // TODO Auto-generated catch block 60 e. printStackTrace (); 61} 62} 63 64/** 65 * download update, 66 */67 protected void checkUpdate () {68 // TODO Auto-generated method stub 69 proDialogShow (LoginActivity. this, "querying... "); 70 RequestParams params = new RequestParams (" url "); 71 x. http (). get (params, new Callback. commonCal Lback <String> () {72 73 @ Override 74 public void onCancelled (CancelledException arg0) {75 // TODO Auto-generated method stub 76 77} 78 79 @ Override 80 public void onError (Throwable arg0, boolean arg1) {81 // TODO Auto-generated method stub 82 PDialogHide (); 83 System. out. println ("network error prompted"); 84} 85 86 @ Override 87 public void onFinished () {88 // TODO Auto-generated method stub 89 90} 91 92 @ Ov Erride 93 public void onSuccess (String arg0) {94 // TODO Auto-generated method stub 95 PDialogHide (); 96 try {97 JSONObject object = new JSONObject (arg0 ); 98 boolean success = object. getBoolean ("succee"); 99 if (success) {100 String desc = object. getString ("desc"); 101 String downloadurl = object. get String ("downloadurl"); 102 String versionname = object. getString ("versionname"); 103 if (nowVersion. Equals (versionname) {104 System. out. println ("the current version is the latest, not new"); 105} else {106 // different, the update prompt dialog box 107 setUpDialog (versionname, downloadurl, desc) appears ); 108} 109} 110} catch (JSONException e) {111 // TODO Auto-generated catch block112 e. printStackTrace (); 113} 114} 115 }); 116} 117 118/** 119*120 * @ param versionname121 * Name of the version in the address 122 * @ param downloadurl123 * download package address 124 * @ param desc125 * description 126 */127 protec Ted void setUpDialog (String versionname, final String downloadurl, 128 String desc) {129 // TODO Auto-generated method stub130 AlertDialog dialog = new AlertDialog. builder (this ). setCancelable (false) 131. setTitle ("Download" + versionname + "version "). setMessage (desc) 132. setNegativeButton ("cancel", null) 133. setPositiveButton ("Download", new DialogInterface. onClickListener () {134 135 @ Override136 public void onClick (Dia LogInterface arg0, int arg1) {137 // TODO Auto-generated method stub138 setDownLoad (downloadurl); 139} 140 }). create (); 141 dialog. show (); 142} 143 144/** 145 * download package 146*147 * @ param downloadurl148 * download url149 * 150 */151 @ SuppressLint ("SdCardPath ") 152 protected void setDownLoad (String downloadurl) {153 // TODO Auto-generated method stub154 RequestParams params = new RequestParams (downloadurl); 155 pa Rams. setAutoRename (true); // resumable download 156 params. setSaveFilePath ("/mnt/sdcard/demo.apk"); 157 x. http (). get (params, new Callback. progressCallback <File> () {158 159 @ Override160 public void onCancelled (CancelledException arg0) {161 // TODO Auto-generated method stub162 163} 164 165 @ Override166 public void onError (Throwable arg0, boolean arg1) {167 // TODO Auto-generated method stub168 if (progressDialog! = Null & progressDialog. isShowing () {169 progressDialog. dismiss (); 170} 171 System. out. println ("update failed"); 172} 173 174 @ Override175 public void onFinished () {176 // TODO Auto-generated method stub177 178} 179 180 @ Override181 public void onSuccess (File arg0) {182 // TODO Auto-generated method stub183 if (progressDialog! = Null & progressDialog. isShowing () {184 progressDialog. dismiss (); 185} 186 Intent intent = new Intent (Intent. ACTION_VIEW); 187 intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); 188 intent. setDataAndType (Uri. fromFile (new File (Environment189. getExternalStorageDirectory (), "demo.apk"), 190 "application/vnd. android. package-archive "); 191 startActivity (intent); 192} 193 194 @ Override195 public void onLoading (Long arg0, long arg1, boolean arg2) {196 // TODO Auto-generated method stub197 progressDialog. setMax (int) arg0); 198 progressDialog. setProgress (int) arg1); 199} 200 201 @ Override202 public void onStarted () {203 // TODO Auto-generated method stub204 System. out. println ("START download"); 205 progressDialog = new ProgressDialog (LoginActivity. (this); 206 progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONT AL); // set it to a horizontal bar to perform the 207 progressDialog. setMessage ("downloading... "); 208 progressDialog. setProgress (0); 209 progressDialog. show (); 210} 211 212 @ Override213 public void onWaiting () {214 // TODO Auto-generated method stub215 216} 217 }); 218} 219 220 private void proDialogShow (Context context, String msg) {221 pDialog = new ProgressDialog (context); 222 pDialog. setMessage (msg); 223 // pDialog. setCancelable (false); 224 PDialog. show (); 225} 226 227 private void PDialogHide () {228 try {229 if (pDialog! = Null & pDialog. isShowing () {230 pDialog. dismiss (); 231} 232} catch (Exception e) {233 e. printStackTrace (); 234} 235} 236}