riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
101
102
103
104
105
106
package cn.flightfeather.thirdappmodule.util.updateApp;
 
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.content.FileProvider;
 
import java.io.File;
import java.io.IOException;
 
/**
 * @author riku
 * apk更新文件下载完成广播接收器
 */
public class DownLoadCompleteReceiver extends BroadcastReceiver {
    private Context context;
 
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
            //在广播中取出下载任务的id
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadManager.Query query=new DownloadManager.Query();
            DownloadManager dm= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            query.setFilterById(id);
            Cursor c = dm.query(query);
            if (c!=null){
                try {
                    if (c.moveToFirst()){
                        //获取文件下载路径
                        String filename = null;
                        //获取下载状态
                        int status = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
                        //安卓7.0以上下载文件uri获取方式
                        String fileUri= c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        //适配安卓8.0
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            if (fileUri != null) {
                                filename = Uri.parse(fileUri).getPath();
                            }
                            //授权“安装未知应用”的许可不成功时,需要动态授权
                            if (!context.getPackageManager().canRequestPackageInstalls()) {
                                Intent intent1 = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + context.getPackageName()));
                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(intent1);
                            }
                        }
                        //适配安卓7.0
                        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            if (fileUri != null) {
                                filename = Uri.parse(fileUri).getPath();
                            }
                        } else {
                            int fileNameIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
                            filename = c.getString(fileNameIdx);
                        }
 
                        //启动安装
                        if (filename != null) {
                            File apkFile = new File(filename);
                            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                                installApk(apkFile);
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    c.close();
                }
 
            }
        }
    }
 
    private void installApk(File file) {
        Intent install=new Intent(Intent.ACTION_VIEW);
        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //启动更新
        Uri uri = null;
        //适配安卓7.0
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", file);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            try {
                Runtime.getRuntime().exec("chmod 777 " + file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            uri = Uri.fromFile(file);
        }
 
        if (uri!=null){
            install.setDataAndType(uri,"application/vnd.android.package-archive");
            context.startActivity(install);
        }
    }
 
}