溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

App應(yīng)用的通用功能

發(fā)布時間:2020-06-24 05:57:50 來源:網(wǎng)絡(luò) 閱讀:534 作者:androidlinyk 欄目:開發(fā)技術(shù)

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

     首先我們要先獲得我們應(yīng)用當(dāng)前版本,接著從服務(wù)器獲得應(yīng)用的最新版本,兩個一比較如果最新版本高于當(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);    

}    



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI