admin
2021-07-28 c0269fcfa876b9c5cf309b2006462b4d09c5ef95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.hanju.video.app.util.downutil;
 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import androidx.core.content.FileProvider;
import android.util.Log;
 
import com.hanju.video.app.BuildConfig;
import com.hanju.video.app.service.DownLoadFileService;
 
import java.io.File;
 
/**
 * @author weikou2015 下载工具类
 */
public class DownFiles extends AsyncTask<String, Integer, String> implements
        DownLoadFile.FileProgressListener {
    private static final String TAG = "DownFiles";
    // TextView tv;
    Context context;
    IProgress progress;
 
    public DownFiles(Context context, IProgress progress) {
        // this.tv = tv;
        this.context = context;
        this.progress = progress;
    }
 
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Log.i(TAG, "下载进度:" + values[0]);
        if (progress != null)
            progress.getProgress(values[0]);
 
        // if (values[0] == 100) {
        // tv.setText("完成");
        // } else {
        // tv.setText("下载" + values[0] + "%");
        // }
 
    }
 
    @Override
    protected String doInBackground(String... params) {
        String url = params[0];
        // 下载
        DownLoadFile dl = new DownLoadFile();
        String[] urls = url.split("/");
        String name = "";
        if (urls.length > 0)
            name = FileUtils.getRootPath(context) + File.separator
                    + urls[urls.length - 1];
        else
            name = FileUtils.getRootPath(context) + File.separator
                    + System.currentTimeMillis() + ".apk";
        System.out.println("APK名称:" + name);
        try {
            File f = dl.downLoadFile(this, name, url, context);
            return f.getPath();
        } catch (Exception e) {
            DownLoadFileService.j = -1;
        }
        return "";
    }
 
    // 完成网络请求
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (!StringUtils.isNullOrEmpty(result) && result.endsWith(".apk")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(result);
 
            //判断是否是AndroidN以及更高的版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } else {
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }
 
    @Override
    public void update(int progress) {
        publishProgress(progress);
    }
 
    public interface IProgress {
        void getProgress(int p);
    }
}