App中有很多通用的功能,如設(shè)置模塊,有緩存、無圖模式、版本更新等一些通用的功能,與大家分享一下其中的版本檢查更新,在我們的App中能自動檢查更新升級。

首先我們要先獲得我們應(yīng)用當(dāng)前版本,接著從服務(wù)器獲得應(yīng)用的最新版本,兩個(gè)一比較如果最新版本高于當(dāng)前版本就同升級更新。
代碼實(shí)現(xiàn):
獲得當(dāng)前版本:
public static int getCurrVersion(Context context){
PackageManager pm = context.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(context
.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
從服務(wù)器獲得最新版本:
public static UpdataInfo getUpdataInfo(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");//設(shè)置解析的數(shù)據(jù)源 7. int type = parser.getEventType();
UpdataInfo info = new UpdataInfo();//實(shí)體 9. 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()); //獲取該文件的信息 18. }
break;
}
type = parser.next();
}
return info;
}
從服務(wù)器下載apk:
1.public static File getFileFromServer(String path, ProgressDialog pd) throws Exception{
//如果相等的話表示當(dāng)前的sdcard掛載在手機(jī)上并且是可用的
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;
//獲取當(dāng)前下載量
pd.setProgress(total);
}
. fos.close();
bis.close();
is.close();
return file;
}
else{
. return null;
}
然后進(jìn)行安裝
protected void installApk(File file) {
Intent intent = new Intent();
//執(zhí)行動作
intent.setAction(Intent.ACTION_VIEW);
//執(zhí)行的數(shù)據(jù)類型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.Android.package-archive");//
startActivity(intent);
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
標(biāo)題名稱:App應(yīng)用的通用功能-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://www.yijiale78.com/article6/csipog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站策劃、靜態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容