app常規功能:檢查更新 異步請求下載apk文件,更新進度條,最後安裝apk

檢查更新是安卓app的常規功能,如今分享一下我作的檢查更新的代碼,事件觸發爲點擊當即更新按鈕。直接上代碼: java

        //建立ProgressDialog對象 m_pDialog = new ProgressDialog(UpdateActivity.this);
	 
	                // 設置進度條風格,風格爲圓形,旋轉的
	                m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	 
	                // 設置ProgressDialog 標題
	                m_pDialog.setTitle("提示");
	                
	                // 設置ProgressDialog 提示信息
	                m_pDialog.setMessage("正在下載最新的apk");
	 
	                // 設置ProgressDialog 的進度條是否不明確
	                m_pDialog.setIndeterminate(false);
	                
	                // 設置ProgressDialog 是否能夠按退回按鍵取消
	                m_pDialog.setCancelable(true);
	                
	                // 讓ProgressDialog顯示
	                m_pDialog.show();
			new DownloadApkAsyncTask().execute("");
/***
	 * 下載新版本apk並安裝
	 * @author zhangda
	 *
	 */
	private class DownloadApkAsyncTask extends AsyncTask<String, Integer, String>{

		@Override
		protected String doInBackground(String... paramArray) {
			String httpUrl = Constant.URL_PREFIX + "app/mkb_" + newVersionName + ".apk";
			final String fileName = "mkb_" + newVersionName + ".apk";
			String path = Environment.getExternalStorageDirectory().getPath();
            File tmpFile = new File(path + "/mkb");
            if (!tmpFile.exists()) {
                    tmpFile.mkdir();
            }
            file = new File(path + "/mkb/" + fileName);

            try {
                URL url = new URL(httpUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                InputStream is = conn.getInputStream();
                //計算文件長度  
                int lenghtOfFile = conn.getContentLength();  
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                conn.connect();
                int len1 = 0;  
                long total = 0;  
                if (conn.getResponseCode() >= 400) {
                        Toast.makeText(UpdateActivity.this, "鏈接超時", Toast.LENGTH_SHORT).show();
                        m_pDialog.cancel();
                } else {
                    while ((len1 = is.read(buf)) > 0) {  
                        total += len1; //total = total + len1
                        System.out.println((int)((total*100)/lenghtOfFile));
                        publishProgress((int)((total*100)/lenghtOfFile));  
                        fos.write(buf, 0, len1);
                    } 
                }

                conn.disconnect();
                fos.close();
                is.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "";
		}
		
		@Override
		protected void onPostExecute(String result) {
			if(file != null){
				m_pDialog.cancel();
				Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                startActivity(intent);
			}
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			System.out.println(values[0]);
			
			if(values[0] < 100){
				m_pDialog.setProgress(values[0]);
			}
			if(values[0] == 100){
				m_pDialog.cancel();
				UpdateActivity.this.finish();
			}
		}
		
	}

相關文章
相關標籤/搜索