編輯:關於android開發
寫的例子比較簡單,是用xutils3.0來進行下載項目更新
1.先通過網絡請求,判斷版本是否要更新
2.若要更新,則彈出一個彈窗,我用的是系統自帶的Dialog,將下載的版本號及下載的內容提示展示出來
3.當用戶點擊下載時,開始下載,下載時展示一個水平的進度條
4.下載完成後,進度條消失,調用系統安裝apk
如下是代碼 用到的xutils的jar包是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 * 設置xutils下載 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 progressDialog; 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 // TODO Auto-generated method stub 42 super.onCreate(savedInstanceState); 43 requestWindowFeature(Window.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 * 下載更新, 66 */ 67 protected void checkUpdate() { 68 // TODO Auto-generated method stub 69 proDialogShow(LoginActivity.this, "正在查詢..."); 70 RequestParams params = new RequestParams("url"); 71 x.http().get(params, new Callback.CommonCallback<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("提示網絡錯誤"); 84 } 85 86 @Override 87 public void onFinished() { 88 // TODO Auto-generated method stub 89 90 } 91 92 @Override 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.getString("downloadurl"); 102 String versionname = object.getString("versionname"); 103 if (nowVersion.equals(versionname)) { 104 System.out.println("當前版本為最新,不用跟新"); 105 } else { 106 // 不同,彈出更新提示對話框 107 setUpDialog(versionname, downloadurl, desc); 108 } 109 } 110 } catch (JSONException e) { 111 // TODO Auto-generated catch block 112 e.printStackTrace(); 113 } 114 } 115 }); 116 } 117 118 /** 119 * 120 * @param versionname 121 * 地址中版本的名字 122 * @param downloadurl 123 * 下載包的地址 124 * @param desc 125 * 版本的描述 126 */ 127 protected void setUpDialog(String versionname, final String downloadurl, 128 String desc) { 129 // TODO Auto-generated method stub 130 AlertDialog dialog = new AlertDialog.Builder(this).setCancelable(false) 131 .setTitle("下載" + versionname + "版本").setMessage(desc) 132 .setNegativeButton("取消", null) 133 .setPositiveButton("下載", new DialogInterface.OnClickListener() { 134 135 @Override 136 public void onClick(DialogInterface arg0, int arg1) { 137 // TODO Auto-generated method stub 138 setDownLoad(downloadurl); 139 } 140 }).create(); 141 dialog.show(); 142 } 143 144 /** 145 * 下載包 146 * 147 * @param downloadurl 148 * 下載的url 149 * 150 */ 151 @SuppressLint("SdCardPath") 152 protected void setDownLoad(String downloadurl) { 153 // TODO Auto-generated method stub 154 RequestParams params = new RequestParams(downloadurl); 155 params.setAutoRename(true);//斷點下載 156 params.setSaveFilePath("/mnt/sdcard/demo.apk"); 157 x.http().get(params, new Callback.ProgressCallback<File>() { 158 159 @Override 160 public void onCancelled(CancelledException arg0) { 161 // TODO Auto-generated method stub 162 163 } 164 165 @Override 166 public void onError(Throwable arg0, boolean arg1) { 167 // TODO Auto-generated method stub 168 if(progressDialog!=null && progressDialog.isShowing()){ 169 progressDialog.dismiss(); 170 } 171 System.out.println("提示更新失敗"); 172 } 173 174 @Override 175 public void onFinished() { 176 // TODO Auto-generated method stub 177 178 } 179 180 @Override 181 public void onSuccess(File arg0) { 182 // TODO Auto-generated method stub 183 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(Environment 189 .getExternalStorageDirectory(), "demo.apk")), 190 "application/vnd.android.package-archive"); 191 startActivity(intent); 192 } 193 194 @Override 195 public void onLoading(long arg0, long arg1, boolean arg2) { 196 // TODO Auto-generated method stub 197 progressDialog.setMax((int)arg0); 198 progressDialog.setProgress((int)arg1); 199 } 200 201 @Override 202 public void onStarted() { 203 // TODO Auto-generated method stub 204 System.out.println("開始下載"); 205 progressDialog = new ProgressDialog(LoginActivity.this); 206 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設置為水平進行條 207 progressDialog.setMessage("正在下載中..."); 208 progressDialog.setProgress(0); 209 progressDialog.show(); 210 } 211 212 @Override 213 public void onWaiting() { 214 // TODO Auto-generated method stub 215 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 }
Android中Dialog對話框,androiddialog布局文件xml: 1 <LinearLayout xmlns:android=http://sc
JSON解析和XML解析對比,JSON解析XML解析 JSON解析和XML解析是較為普遍的兩種解析方式,其中JSON解析的市場分額更大
Android Context完全解析,你所不知道的Context的各種細節 前幾篇文章,我也是費勁心思寫了一個ListView系列的三部曲,雖然在內容上可
算法—5.快速排序,算法排序快速排序可能是應用最廣泛的排序算法了。流行的原因是它實現簡單、適用於各種不同的輸入數據且在一般應用中比其他排序算法都要快得多。快速排序引人注目