安卓開發下載文件和安裝文件

項目做到版本更新遇到下載和安裝 代碼記一下

1:文件下載 

三個全局變量

    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    InputStream is = null;

下載

    public void downloadApktoappDir(String urlstr, String apkname)
            throws IOException {
        if (apkname == null || "".equals(apkname))
            apkname = urlstr.substring(urlstr.lastIndexOf("/") + 1);
        urlstr = AiChuXingApplication.ATTR + urlstr;
        URL url = null;
        
        try {
            url = new URL(urlstr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            // 獲取到文件的大小
            int size = conn.getContentLength();
            is = conn.getInputStream();
            fos = openFileOutput(apkname, Context.MODE_WORLD_READABLE);
            bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            int total = 0;
            while ((len = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                // 獲取當前下載量
                total += len;
                Message msg = Message.obtain();
                msg.what = 1;
                msg.obj = total*100/size;
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != fos) {
                fos.close();
                fos = null;
            }
            if (null != bis) {
                bis.close();
                bis = null;
            }
            if (null != is) {
                is.close();
                is = null;
            }
        }
    }

handler顯示下載進度

            case 1:
                if (null != downloaddialog) {
                    if (null != emstx) {
                        if (((Integer)msg.obj).intValue() >=100) {
                            downloaddialog.dismiss();
                            downloaddialog = null;
                            showInstallDialog();
                        } else {
                            emstx.setProgress(((Integer)msg.obj).intValue());
                        }
                    }
                }
                break;

2:安裝

    public void installApkFromLocalPath(String apkname) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://"
                        + getApplicationContext().getFilesDir()
                                .getAbsolutePath() + "/" + apkname),
                "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

3:權限

    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.INTERNET" />


備註:下載文件存放在data/data/packagename/file文件夾下,並沒有放在SDCARD中

完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章