zoukankan      html  css  js  c++  java
  • Android应用自动更新功能的实现!

    Android应用自动更新功能的实现!
    http://blog.csdn.net/android_tutor/article/details/7015986

    private static final int DOWN_UPDATE = 1;
    private static final int DOWN_OVER = 2;
    private String apkUrl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";  
    
    private static String saveFileName = savePath + "";
    
    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;        
            }
        }
    };
    
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View v = inflater.inflate(R.layout.progress, null);
    
    private Runnable mdownApkRunnable = new Runnable() {
        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 file = new File(savePath);    
                if (!file.exists()) {
                    file.mkdir();
                }*/
                String apkFile = saveFileName;
                File ApkFile = new File(apkFile);
                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);
                        break;
                    }
                    fos.write(buf, 0, numRead);
                } while (!interceptFlag);
    
                fos.close();
                is.close();
    
            } catch (MalformedURLException e) {
                e.printStackTrace(); 
            } catch (IOException  e) {
                e.printStackTrace(); 
            }
        }
    }
    
    private void installApk() {
        File apkFile = new File(saveFileName);
        if (!apkFile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
        mContext.startActivity(i);
    }
  • 相关阅读:
    poj 1084 Brainman(归并排序)
    Poj 2299 Ultra-QuickSort(归并排序)
    poj 1068 Parencodings(栈)
    Poj 2499 Binary Tree(贪心)
    Poj 2255 Tree Recovery(二叉搜索树)
    poj 2021 Relative Relatives(暴力)
    Poj 2092 Grandpa is Famous(基数排序)
    解决UMeditor上传图片失败
    解决使用了属性overflow:scroll、overflow-y:scroll、overflow-x:scroll;的网页在iPhone iOS Safari浏览器中滑动不流畅问题
    Kindeditor上传图片报错
  • 原文地址:https://www.cnblogs.com/bluestorm/p/3741062.html
Copyright © 2011-2022 走看看