Android版本迭代
1.先來說一下實現思路:每次啓動應用我們就獲取放在服務器上的更新日誌(最好保存了最新的版本號,更新內容說明,apk下載地址),我們獲取到版本號與當前應用的版本好進行對比,這樣我們就可以知道應用是否更新了,廢話不多說直接上代碼。。
先來說說versionCode和versionName
versionCode 1//對消費者不可見,僅用於應用市場、程序內部識別版本,判斷新舊等用途。 versionName "1.0"//展示給消費者,消費者會通過它認知自己安裝的版本.
//更新版本修改versionCode的值,必須是int哦
2.獲取服務器上的更新日誌信息,需要放在線程中執行哦!
//版本升級apk的地址
private final String path = "http://www.zgylzb.com/.../update.asp";
/**
* 獲取升級信息
* @return
* @throws Exception
*/
public UpdateInfo getUpDateInfo() throws Exception {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader reader = null;
UpdateInfo updateInfo;
try {
// 創建一個url對象
URL url = new URL(path);
// 通過url對象,創建一個HttpURLConnection對象(連接)
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(3000);
if (urlConnection.getResponseCode() == 200) {
// 通過HttpURLConnection對象,得到InputStream
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String info = sb.toString();
//對升級的信息進行封裝
updateInfo = new UpdateInfo();
//版本
updateInfo.setVersion(info.split("&")[1]);
//升級說明
updateInfo.setDescription(info.split("&")[2]);
//apk下載地址
updateInfo.setUrl(info.split("&")[3]);
return updateInfo;
}
} catch (UnknownHostException e) {
return null;//連接超時
} catch (Exception e) {
e.printStackTrace();//網絡請求出錯
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
服務器返回的更新數據,最好就使用JSON返回。
3.這裏進行網絡訪問所以自行添加Internet權限,同時還需要對手機網絡進行判斷等一系列處理這裏就不貼出來了。獲取到了版本信息就需要與當前版本進行對比了
if (isNeedUpdate()) {
showUpdateDialog(); //需要更新版本
}
/**
* 判斷apk是否需要升級
*
* @return true需要| false不需要
*/
private boolean isNeedUpdate() {
// 最新版本的版本號,info就是上面封裝了更新日誌信息的對象
int v = Integer.parseInt(info.getVersion());
if (server > getVersion())) {
//需要升級
return true;
} else {
//不需要升級,直接啓動啓動Activity
return false;
}
}
//獲取當前版本號
private int getVersion() {
try {
PackageManager packageManager = getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
4.需要進行升級,那就把升級信息用對話框展示給用戶
/**
* 顯示升級信息的對話框
*/
private void showUpdateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("請升級APP版本至" + info.getVersion());
builder.setMessage(info.getDescription());
builder.setCancelable(false);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downFile(info.getUrl());//點擊確定將apk下載
} else {
Toast.makeText(WelcomeActivity.this, "SD卡不可用,請插入SD卡", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//用戶點擊了取消
}
});
builder.create().show();
}
5.下載最新版本的apk
/**
* 下載最新版本的apk
*
* @param path apk下載地址
*/
private void downFile(final String path) {
pBar = new ProgressDialog(WelcomeActivity.this);
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pBar.setCancelable(false);
pBar.setTitle("正在下載...");
pBar.setMessage("請稍候...");
pBar.setProgress(0);
pBar.show();
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
con.setRequestProperty("Charset", "UTF-8");
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
int length = con.getContentLength();// 獲取文件大小
InputStream is = con.getInputStream();
pBar.setMax(length); // 設置進度條的總長度
FileOutputStream fileOutputStream = null;
if (is != null) {
//對apk進行保存
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "home.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
process += ch;
pBar.setProgress(process); // 實時更新進度了
}
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
//apk下載完成,使用Handler()通知安裝apk
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
6.對apk進行安裝
//將下載進度對話框取消
pBar.cancel();
//安裝apk,也可以進行靜默安裝
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "home.apk")),
"application/vnd.android.package-archive");
startActivityForResult(intent, 10);
版本更新基本是就這幾個步驟,具體實現還是需要根據公司需求。
先來說說versionCode和versionName
versionCode 1//對消費者不可見,僅用於應用市場、程序內部識別版本,判斷新舊等用途。 versionName "1.0"//展示給消費者,消費者會通過它認知自己安裝的版本.
//更新版本修改versionCode的值,必須是int哦
2.獲取服務器上的更新日誌信息,需要放在線程中執行哦!
//版本升級apk的地址
private final String path = "http://www.zgylzb.com/.../update.asp";
/**
* 獲取升級信息
* @return
* @throws Exception
*/
public UpdateInfo getUpDateInfo() throws Exception {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader reader = null;
UpdateInfo updateInfo;
try {
// 創建一個url對象
URL url = new URL(path);
// 通過url對象,創建一個HttpURLConnection對象(連接)
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(3000);
if (urlConnection.getResponseCode() == 200) {
// 通過HttpURLConnection對象,得到InputStream
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String info = sb.toString();
//對升級的信息進行封裝
updateInfo = new UpdateInfo();
//版本
updateInfo.setVersion(info.split("&")[1]);
//升級說明
updateInfo.setDescription(info.split("&")[2]);
//apk下載地址
updateInfo.setUrl(info.split("&")[3]);
return updateInfo;
}
} catch (UnknownHostException e) {
return null;//連接超時
} catch (Exception e) {
e.printStackTrace();//網絡請求出錯
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
服務器返回的更新數據,最好就使用JSON返回。
3.這裏進行網絡訪問所以自行添加Internet權限,同時還需要對手機網絡進行判斷等一系列處理這裏就不貼出來了。獲取到了版本信息就需要與當前版本進行對比了
if (isNeedUpdate()) {
showUpdateDialog(); //需要更新版本
}
/**
* 判斷apk是否需要升級
*
* @return true需要| false不需要
*/
private boolean isNeedUpdate() {
// 最新版本的版本號,info就是上面封裝了更新日誌信息的對象
int v = Integer.parseInt(info.getVersion());
if (server > getVersion())) {
//需要升級
return true;
} else {
//不需要升級,直接啓動啓動Activity
return false;
}
}
//獲取當前版本號
private int getVersion() {
try {
PackageManager packageManager = getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
4.需要進行升級,那就把升級信息用對話框展示給用戶
/**
* 顯示升級信息的對話框
*/
private void showUpdateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("請升級APP版本至" + info.getVersion());
builder.setMessage(info.getDescription());
builder.setCancelable(false);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downFile(info.getUrl());//點擊確定將apk下載
} else {
Toast.makeText(WelcomeActivity.this, "SD卡不可用,請插入SD卡", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//用戶點擊了取消
}
});
builder.create().show();
}
5.下載最新版本的apk
/**
* 下載最新版本的apk
*
* @param path apk下載地址
*/
private void downFile(final String path) {
pBar = new ProgressDialog(WelcomeActivity.this);
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pBar.setCancelable(false);
pBar.setTitle("正在下載...");
pBar.setMessage("請稍候...");
pBar.setProgress(0);
pBar.show();
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
con.setRequestProperty("Charset", "UTF-8");
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
int length = con.getContentLength();// 獲取文件大小
InputStream is = con.getInputStream();
pBar.setMax(length); // 設置進度條的總長度
FileOutputStream fileOutputStream = null;
if (is != null) {
//對apk進行保存
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "home.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
process += ch;
pBar.setProgress(process); // 實時更新進度了
}
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
//apk下載完成,使用Handler()通知安裝apk
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
6.對apk進行安裝
//將下載進度對話框取消
pBar.cancel();
//安裝apk,也可以進行靜默安裝
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "home.apk")),
"application/vnd.android.package-archive");
startActivityForResult(intent, 10);
版本更新基本是就這幾個步驟,具體實現還是需要根據公司需求。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.