• 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);
    }
  • 相关阅读:
    python 数据结构 图解递归 海龟画图分形树
    python 数据结构 理解迭代与递归 递归的实例 栈帧 函数调用
    python 数据结构 双端队列的两种实现方式 list 和 DoublelinkedList 回文词验证
    python 数据结构 实现队列的几种方法
    python 数据结构 实现链队的两种方法
    python 数据结构 查找数组最值
    python 数据结构 队列的实例 回文词与双端队列
    双向链表练习题
    均分纸牌问题
    L1-8 估值一亿的AI核心代码
  • 原文地址:https://www.cnblogs.com/bluestorm/p/3741062.html
Copyright © 2020-2023  润新知