日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

最新开发android版本,Android版本检测升级

發布時間:2023/12/3 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最新开发android版本,Android版本检测升级 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們應該都有類似的使用體驗,當一款APP需要更新是,進入界面會提醒有新的更新是否更新,這里有那么幾個步驟

1、首先檢測當前版本

2、判斷服務器中版本

3、如果有更新則點擊更新,下載安裝包,下載完成后自動安裝

具體代碼怎么實現呢?下面我們一起看一下

/*

* 獲取當前程序的版本號

*/

private String getVersionName() throws Exception{

//獲取packagemanager的實例

PackageManager packageManager = getPackageManager();

//getPackageName()是你當前類的包名,0代表是獲取版本信息

PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);

return packInfo.versionName;

}

讀取服務器版本號

/*

* 用pull解析器解析服務器返回的xml文件 (xml封裝了版本號)

*/

public static UpdataInfo getUpdataInfo(InputStream is) throws Exception{

XmlPullParser parser = Xml.newPullParser();

parser.setInput(is, "utf-8");//設置解析的數據源

int type = parser.getEventType();

UpdataInfo info = new UpdataInfo();//實體

while(type != XmlPullParser.END_DOCUMENT ){

switch (type) {

case XmlPullParser.START_TAG:

if("version".equals(parser.getName())){

info.setVersion(parser.nextText()); //獲取版本號

}else if ("url".equals(parser.getName())){

info.setUrl(parser.nextText()); //獲取要升級的APK文件

}else if ("description".equals(parser.getName())){

info.setDescription(parser.nextText()); //獲取該文件的信息

}

break;

}

type = parser.next();

}

return info;

}

下載

public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{

//如果相等的話表示當前的sdcard掛載在手機上并且是可用的

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

//獲取到文件的大小

pd.setMax(conn.getContentLength());

InputStream is = conn.getInputStream();

File file = new File(Environment.getExternalStorageDirectory(), "updata.apk");

FileOutputStream fos = new FileOutputStream(file);

BufferedInputStream bis = new BufferedInputStream(is);

byte[] buffer = new byte[1024];

int len ;

int total=0;

while((len =bis.read(buffer))!=-1){

fos.write(buffer, 0, len);

total+= len;

//獲取當前下載量

pd.setProgress(total);

}

fos.close();

bis.close();

is.close();

return file;

}

else{

return null;

}

}

版本匹配、自動安裝

/*

* 從服務器獲取xml解析并進行比對版本號

*/

public class CheckVersionTask implements Runnable{

public void run() {

try {

//從資源文件獲取服務器 地址

String path = getResources().getString(R.string.serverurl);

//包裝成url的對象

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

InputStream is =conn.getInputStream();

info = UpdataInfoParser.getUpdataInfo(is);

if(info.getVersion().equals(versionname)){

Log.i(TAG,"版本號相同無需升級");

LoginMain();

}else{

Log.i(TAG,"版本號不同 ,提示用戶升級 ");

Message msg = new Message();

msg.what = UPDATA_CLIENT;

handler.sendMessage(msg);

}

} catch (Exception e) {

// 待處理

Message msg = new Message();

msg.what = GET_UNDATAINFO_ERROR;

handler.sendMessage(msg);

e.printStackTrace();

}

}

}

Handler handler = new Handler(){

@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

super.handleMessage(msg);

switch (msg.what) {

case UPDATA_CLIENT:

//對話框通知用戶升級程序

showUpdataDialog();

break;

case GET_UNDATAINFO_ERROR:

//服務器超時

Toast.makeText(getApplicationContext(), "獲取服務器更新信息失敗", 1).show();

LoginMain();

break;

case DOWN_ERROR:

//下載apk失敗

Toast.makeText(getApplicationContext(), "下載新版本失敗", 1).show();

LoginMain();

break;

}

}

};

/*

*

* 彈出對話框通知用戶更新程序

*

* 彈出對話框的步驟:

* 1.創建alertDialog的builder.

* 2.要給builder設置屬性, 對話框的內容,樣式,按鈕

* 3.通過builder 創建一個對話框

* 4.對話框show()出來

*/

protected void showUpdataDialog() {

AlertDialog.Builder builer = new Builder(this) ;

builer.setTitle("版本升級");

builer.setMessage(info.getDescription());

//當點確定按鈕時從服務器上下載 新的apk 然后安裝

builer.setPositiveButton("確定", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

Log.i(TAG,"下載apk,更新");

downLoadApk();

}

});

//當點取消按鈕時進行登錄

builer.setNegativeButton("取消", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

LoginMain();

}

});

AlertDialog dialog = builer.create();

dialog.show();

}

/*

* 從服務器中下載APK

*/

protected void downLoadApk() {

final ProgressDialog pd; //進度條對話框

pd = new ProgressDialog(this);

pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

pd.setMessage("正在下載更新");

pd.show();

new Thread(){

@Override

public void run() {

try {

File file = DownLoadManager.getFileFromServer(info.getUrl(), pd);

sleep(3000);

installApk(file);

pd.dismiss(); //結束掉進度條對話框

} catch (Exception e) {

Message msg = new Message();

msg.what = DOWN_ERROR;

handler.sendMessage(msg);

e.printStackTrace();

}

}}.start();

}

//安裝apk

protected void installApk(File file) {

Intent intent = new Intent();

//執行動作

intent.setAction(Intent.ACTION_VIEW);

//執行的數據類型

intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

startActivity(intent);

}

/*

* 進入程序的主界面

*/

private void LoginMain(){

Intent intent = new Intent(this,MainActivity.class);

startActivity(intent);

//結束掉當前的activity

this.finish();

}

相關類

public class UpdataInfo {

private String version;

private String url;

private String description;

public String getVersion() {

return version;

}

public void setVersion(String version) {

this.version = version;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

相關布局

http://192.168.0.64:8080/mobilesafe.apk

檢測到最新版本,請及時更新!

總結

以上是生活随笔為你收集整理的最新开发android版本,Android版本检测升级的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。