123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package com.edufound.android.xyyf.util;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- import android.view.Display;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.edufound.android.xyyf.R;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- public class UpdateUtil {
- String mNewAppFile;
- int downLoadFileSize;
- Activity mContext;
- File mApkDir;
- int fileSize;
- ProgressBar pBar;
- TextView tText;
- public UpdateUtil(Activity context) {
- mContext = context;
- mApkDir = context.getDir("apk", Context.MODE_PRIVATE);
- showWindow();
- }
- //显示对话框
- private void showWindow() {
- mDownHandler.sendEmptyMessage(0x2283);
- }
- // 下載文件
- public void down_file(String url) throws IOException {
- String filename = url.substring(url.lastIndexOf("/") + 1);
- URL myURL = new URL(url);
- URLConnection conn = myURL.openConnection();
- conn.connect();
- InputStream is = conn.getInputStream();
- fileSize = conn.getContentLength();
- if (fileSize <= 0)
- throw new RuntimeException("filesize is 0");
- if (is == null)
- throw new RuntimeException("stream is null");
- mNewAppFile = mApkDir.toString() + "/" + filename;
- FileOutputStream fos = new FileOutputStream(mNewAppFile);
- byte buf[] = new byte[1024];
- downLoadFileSize = 0;
- sendMsg(0);
- do {
- int numread = is.read(buf);
- if (numread == -1) {
- break;
- }
- fos.write(buf, 0, numread);
- downLoadFileSize += numread;
- sendMsg(1);//
- } while (true);
- sendMsg(2);//
- try {
- is.close();
- } catch (Exception ex) {
- Logger.e("下载文件异常--" + ex.getMessage());
- }
- }
- private void sendMsg(int flag) {
- Message msg = new Message();
- msg.what = flag;
- mDownHandler.sendMessage(msg);
- }
- Dialog dialog;
- // 更新下载
- private Handler mDownHandler = new Handler(Looper.getMainLooper()) {
- @Override
- public void handleMessage(Message msg) {
- if (!Thread.currentThread().isInterrupted()) {
- switch (msg.what) {
- case 0x2283:
- FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.window_upgrade, null);
- pBar = (ProgressBar) frameLayout.findViewById(R.id.upgrade_progress);
- tText = (TextView) frameLayout.findViewById(R.id.upgrade_text);
- dialog = new Dialog(mContext);
- dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
- return true;
- }
- return false;
- }
- });
- dialog.setContentView(frameLayout);
- Window dialogWindow = dialog.getWindow();
- dialogWindow.setGravity(Gravity.CENTER);
- WindowManager m = mContext.getWindowManager();
- Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
- WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
- p.height = (int) (d.getHeight() * 0.6);
- p.width = (int) (d.getWidth() * 0.8);
- dialogWindow.setAttributes(p);
- dialog.setTitle("升级中");
- dialog.setCanceledOnTouchOutside(false);
- dialog.show();
- break;
- case 0:
- pBar.setMax(fileSize);
- case 1:
- pBar.setProgress(downLoadFileSize);
- int result = downLoadFileSize * 100 / fileSize;
- tText.setText((downLoadFileSize / 1024 / 1024) + "MB/" + (fileSize / 1024 / 1024) + "MB");
- if (result == 100) {
- }
- break;
- case 2:
- dialog.dismiss();
- EduFoundUtil.openFile(mContext, new File(mNewAppFile));
- break;
- case -1:
- String error = msg.getData().getString("error");
- Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
- break;
- }
- }
- super.handleMessage(msg);
- }
- };
- }
|