admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
package com.lcjian.library.upgrade;
 
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import androidx.core.content.FileProvider;
import android.util.Log;
import android.webkit.MimeTypeMap;
 
import java.io.File;
 
@SuppressLint("NewApi")
public class UpdateService extends IntentService {
 
    private long mDownLoadId;
 
    public UpdateService() {
        super("UpdateService");
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(intent.getStringExtra("download_url")));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        //设置文件类型  
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(intent.getStringExtra("download_url")));
        request.setMimeType(mimeString);
        //在通知栏中显示   
        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
        request.setVisibleInDownloadsUi(true);
        // sdcard的目录下的download文件夹  
        request.setDestinationInExternalPublicDir("/download/", intent.getStringExtra("file_name"));
        request.setTitle(intent.getStringExtra("notification_title"));
        request.setDescription(intent.getStringExtra("notification_description"));
        mDownLoadId = downloadManager.enqueue(request);
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
 
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听
            if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) == mDownLoadId) {
                queryDownloadStatus(context, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
            }
        }
    };
 
    private void queryDownloadStatus(Context context, long downloadId) {
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadId);
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch (status) {
                case DownloadManager.STATUS_PAUSED:
                    Log.v("DownloadManager", "STATUS_PAUSED");
                case DownloadManager.STATUS_PENDING:
                    Log.v("DownloadManager", "STATUS_PENDING");
                case DownloadManager.STATUS_RUNNING:
                    // 正在下载,不做任何事情
                    Log.v("DownloadManager", "STATUS_RUNNING");
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    File file = new File("file://" + c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
                    //判断是否是AndroidN以及更高的版本
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".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);
                    // 完成
                    Log.v("DownloadManager", "STATUS_SUCCESSFUL");
                    break;
                case DownloadManager.STATUS_FAILED:
                    // 清除已下载的内容,重新下载
                    Log.v("DownloadManager", "STATUS_FAILED");
                    downloadManager.remove(downloadId);
                    break;
            }
        }
    }
 
}