package com.crea.logic; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.pm.PackageManager.NameNotFoundException; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import com.crea.R; public class UpdateManager { private Context mContext; //新版本名称 private String newVerName = ""; //新版本号 private int newVerCode = -1; //返回的安装包url private String apkUrl = "http://www.d5jr.com:8080/CUFinance/Can.apk"; private Dialog noticeDialog; private Dialog downloadDialog; /* 下载安装包名字 */ private static final String saveFileName = "UpdateDemoRelease.apk"; /* 进度条与通知ui刷新的handler和msg常量 */ private ProgressBar mProgress; private static final int DOWN_UPDATE = 1; private static final int DOWN_OVER = 2; private int progress; private Thread downLoadThread; private boolean interceptFlag = false; private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case DOWN_UPDATE: mProgress.setProgress(progress); break; case DOWN_OVER: installApk(); break; default: break; } }; }; public UpdateManager(Context context) { this.mContext = context; } //外部接口让主Activity调用 public void checkUpdateInfo(){ if(getServerVer()){ int verCode = this.getVerCode(mContext); if(newVerCode>verCode){ //更新版本 showNoticeDialog(); }else{ //notNewVersionUpdate();//提示已是最新版本 } } } private void showNoticeDialog(){ AlertDialog.Builder builder = new Builder(mContext); String verName = this.getVerName(mContext); StringBuffer sb = new StringBuffer(); sb.append(verName+"发现版本:"); sb.append(newVerName); sb.append(",是否更新"); builder.setTitle("软件版本更新"); builder.setMessage(sb.toString()); builder.setPositiveButton("下载", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); showDownloadDialog(); } }); builder.setNegativeButton("以后再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); noticeDialog = builder.create(); noticeDialog.show(); } private void showDownloadDialog(){ AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("软件版本更新"); final LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.progress, null); mProgress = (ProgressBar)v.findViewById(R.id.progress); builder.setView(v); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); interceptFlag = true; } }); downloadDialog = builder.create(); downloadDialog.show(); downloadApk(); } private Runnable mdownApkRunnable = new Runnable() { @Override public void run() { try { URL url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); File ApkFile = new File(Environment.getExternalStorageDirectory(),saveFileName); FileOutputStream fos = new FileOutputStream(ApkFile); int count = 0; byte buf[] = new byte[1024]; do{ int numread = is.read(buf); count += numread; progress =(int)(((float)count / length) * 100); //更新进度 mHandler.sendEmptyMessage(DOWN_UPDATE); if(numread <= 0){ //下载完成通知安装 mHandler.sendEmptyMessage(DOWN_OVER); downloadDialog.dismiss(); noticeDialog.dismiss(); break; } fos.write(buf,0,numread); }while(!interceptFlag);//点击取消就停止下载. fos.close(); is.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } }; /** * 不更新版本 */ public void notNewVersionUpdate(){ String verName = this.getVerName(mContext); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append("\n已是最新版本,无需更新"); Dialog dialog = new AlertDialog.Builder(mContext) .setTitle("版本更新") .setMessage(sb.toString()) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create(); dialog.show(); } /** * 下载apk * @param url */ private void downloadApk(){ downLoadThread = new Thread(mdownApkRunnable); downLoadThread.start(); } /** * 安装apk * @param url */ private void installApk(){ Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),saveFileName)) , "application/vnd.android.package-archive"); mContext.startActivity(i); } /** * 获得版本号 */ public int getVerCode(Context context){ int verCode = -1; try { verCode = context.getPackageManager().getPackageInfo("com.crea", 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return verCode; } /** * 获得版本名称 */ public String getVerName(Context context){ String verName = ""; try { verName = context.getPackageManager().getPackageInfo("com.crea", 0).versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return verName; } /** * 从服务器端获得版本号与版本名称 * @return */ public boolean getServerVer(){ try { /* URL url = new URL("http://10.0.2.2:8080/ApkUpdateService/ver"); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); httpConnection.setRequestMethod("GET"); httpConnection.connect(); InputStreamReader reader = new InputStreamReader(httpConnection.getInputStream()); BufferedReader bReader = new BufferedReader(reader); String json = bReader.readLine(); JSONArray array = new JSONArray(json); JSONObject jsonObj = array.getJSONObject(0); newVerCode = Integer.parseInt(jsonObj.getString("verCode")); newVerName = jsonObj.getString("verName");*/ newVerCode = 2; newVerName = "宝汇理财2.0"; } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
当一个人找不到出路的时候,最好的办法就是将当前能做好的事情做到极致,做到无人能及。