Android 實現(xiàn)版本更新
步驟:
1、獲取已安裝的軟件版本號;
View Code
/** * 獲得軟件版本號 * * @param context * @return */ public String getVerName(Context context) { String verName = ""; try { verName = context.getPackageManager().getPackageInfo( "com.ichances.zhongyue", 0).versionName; } catch (NameNotFoundException e) { Log.e("獲取版本信息出錯", e.getMessage()); } return verName; }
2、通過WebService將參數(shù)發(fā)送給服務(wù)端,在服務(wù)端進行匹配,返回相應的Map;
3、若需要更新,通過Map中下載Url,將apk下載下來;
View Code
/** * 下載版本文件 */ private void doNewVersionUpdate() { String msg = "發(fā)現(xiàn)最新版本" + newVerName + ",是否更新?"; new AlertDialog.Builder(EvenMoreActivity.this) .setTitle("軟件更新提示") .setMessage(msg) // 設(shè)置內(nèi)容 .setPositiveButton("更新",// 設(shè)置確定按鈕 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { downFile(downUrl); dialog.dismiss(); } }) .setNegativeButton("暫不更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }).create().show(); } void downFile(final String url) { new Thread() { public void run() { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); FileOutputStream fileOutputStream = null; if (is != null) { File storeDir = new File(AppSession.storePath); if (!storeDir.exists()) { storeDir.mkdirs(); } File file = new File(AppSession.storePath, packageName); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch = -1; while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } update(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); }
4、啟動安裝Intent,即可
View Code


浙公網(wǎng)安備 33010602011771號