The Android app version update download app function only needs the download address of the apk

1. Please see the code. Here I give a method to download apk, this method needs to pass the download address of apk

   private String appUrl; //应用升级地址
    private String appName="xxxxx"; //应用名称
    private String updateFile; //最新APK的路径

    private HttpURLConnection connection;
    private final int NEW_MESSAGE_ID = 0;

    /**
     * 下载更新APK文件
     *
     * @param appUrl    apk的下载地址
     * @return   自己定义的消息通知
     */
    private int downloadUpdateFile(final String appUrl) {
    
    
        try {
    
    

            //设置进度条操作
            URL url = new URL(appUrl);
            //打开和URL之间的连接
            connection = (HttpURLConnection) url.openConnection();
            //设置网络请求
            connection.setRequestMethod("GET");
            //开始读取服务器端数据,到了指定时间还没有读到数据,则报超时异常
            connection.setReadTimeout(50000);
            //要取得长度,要求http请求不要gzip压缩
            connection.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码

            //建立实际的连接
            connection.connect();
            //这里开启一个子线程,如果要更新UI,请切换到主线程
            new Thread(new Runnable() {
    
    
                @Override
                public void run() {
    
    
                    int total_length = 0;
                    BufferedOutputStream bos = null;
                    BufferedInputStream bis = null;
                    try {
    
    
                        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    
    
                            InputStream is = connection.getInputStream();
                            bis = new BufferedInputStream(is);

                            Log.d("111333", "走到版本更新这里,需要更新,打印文件路径="+getApplicationContext().getExternalFilesDir(null).getPath());

                            //在这里设置apk下载后保存的绝对路径
                      //      String path= UpdateUtils.getDiskCacheDir(mContext,"xupdate")+ File.separator + checkVersionResult.getVersionName();
                     //       File cacheDir = new File(path);
                     //       Log.d("111333", "走到版本更新这里,需要更新,打印新的文件路径="+path);

                            //创建文件路径
                            File cacheDir = new File(getApplicationContext().getExternalFilesDir(null).getPath());
                            if (!cacheDir.exists()) {
    
    
                                cacheDir.mkdirs();
                            }
                            //创建文件夹
                            //通过输出流,下载到指定的本地文件目录
                            File file = new File(cacheDir, appName + ".apk");
                            if (!file.exists()) {
    
    
                                file.createNewFile();
                            }
                            //检查SD卡的状态是否可用
                            //    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    
    
                            //   Toast.makeText(UpdateService.this, "SD卡不可用~", Toast.LENGTH_SHORT).show();
                            //     }

                            FileOutputStream fos = new FileOutputStream(file);
                            bos = new BufferedOutputStream(fos);

                            //获取文件流大小,更新进度
                            byte[] buffer = new byte[1024 * 8];
                            int len;
                            int pro1 = 0;
                            long file_length = connection.getContentLength();
                            while ((len = bis.read(buffer)) != -1) {
    
    
                                bos.write(buffer, 0, len);
                                total_length += len;
                                if (file_length > 0) {
    
    

                                    pro1 = (int) ((total_length / (float) file_length) * 100);//进度条传递进度
                                    //       builder.setProgress(100, pro1, false);
                                    Log.d("111333", "走到下载" + pro1 + "%");

                                    //        builder.setContentText("下载" + pro1 + "%");
                                }
                            }
                            //    builder.setStyle(new NotificationCompat.BigTextStyle().bigText("下载完成,点击安装")); //显示多行文本

                            updateFile = file.getAbsolutePath();
                            
                            Log.d("111333", "下载路径:" + updateFile);

                        }

                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    } finally {
    
    
                        //关闭资源,先关闭外层流,在关闭内层流
                        try {
    
    
                            if (bis != null) {
    
    
                                bis.close();
                            }
                        } catch (IOException e) {
    
    
                            e.printStackTrace();
                        }
                        try {
    
    
                            if (bos != null) {
    
    
                                bos.close();
                            }
                        } catch (IOException e) {
    
    
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return NEW_MESSAGE_ID;
    }

Guess you like

Origin blog.csdn.net/qq_36570506/article/details/131602393