mac
2023-11-28 31d32567ce92d2a3bc77865a6a1cec2635c9dc46
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 com.hdl.photovoltaic.widget.apkwgtupload;
 
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
 
import androidx.core.app.NotificationCompat;
 
import org.jetbrains.annotations.Nullable;
 
/**
 * Created by Zoro
 * Created on 2021/5/25
 * description:
 */
public class ApkDownLoadService extends Service {
 
 
    private Context context;
 
    /**
     * apk更新管理类
     */
    private AppDownloadManager manager;
 
    /**
     * 更新Url
     */
    private String updateUrl;
 
    /**
     * 下载文件名
     */
    private String downLoadName;
 
    /**
     * 下载文件类型  1:wgt   0:apk
     */
    private String downLoadType;
 
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        manager = new AppDownloadManager(context);
        startForeground(System.identityHashCode(this), getNotification());
        if (null != manager) {
            manager.resume();
        }
    }
 
    private Notification getNotification() {
        final String CHANNEL_ID = "default";
        final String CHANNEL_NAME = "Default Channel";
        NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(NOTIFICATION_SERVICE);
 
        //适配安卓8.0的消息渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager != null) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(channel);
            }
        }
 
        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(getBaseContext(), CHANNEL_ID);
 
        notification.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL);
 
        return notification.build();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        App.getApp().setShowUpdate(true);
        updateUrl = intent.getStringExtra("updateUrl");
        downLoadName = intent.getStringExtra("downLoadName");
        downLoadType = intent.getStringExtra("downLoadType");
        if (null != updateUrl) {
            if (null != manager) {
                manager.downloadApk(updateUrl, downLoadName, "update...", downLoadName, downLoadType);
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.onPause();
        }
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}