admin
2020-10-10 8039a1b2fbfa3471b6f726d3e839d7867c81a84f
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
107
108
109
110
111
112
113
114
115
116
117
package com.ysh.wpc.appupdate.service;
 
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.RemoteViews;
import android.widget.Toast;
 
import com.ysh.wpc.appupdate.R;
import com.ysh.wpc.appupdate.download.DownLoadApks;
import com.ysh.wpc.appupdate.download.DownLoadApks.IProgress;
 
public class DownLoadFileService extends Service {
    private NotificationManager manager;
    private Notification notif;
    public static int j = -1;
    private String downloadUrl = "";// apk涓嬭浇璺緞
    private String apkType;
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @SuppressLint("NewApi")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle bundle = (Bundle) intent.getExtras();
        downloadUrl = bundle.getString("downloadurl", "");
        apkType = "";
        new DownLoadApks(this, new IProgress() {
 
            @Override
            public void getProgress(int p) {
                // stub
                if (p == 100) {
                    handler.sendEmptyMessage(1);
                } else {
                    if (manager == null || notif == null) {
                        Toast.makeText(DownLoadFileService.this,
                                "插件开始下载······", Toast.LENGTH_SHORT).show();
                        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        notif = new Notification();
                        // notif.largeIcon
                        // ReadAssetsImage image = new ReadAssetsImage();
                        // notif.largeIcon = image.getImageFromAssetsFile(
                        // DownLoadFileService.this, "ic_launcher");
                        notif.tickerText = "正在更新中";
                        notif.icon = R.drawable.ic_launcher;
                        // 通知栏显示所用到的布局文件
                        notif.contentView = new RemoteViews(getPackageName(),
                                R.layout.notify_item);
                    }
                    Message msg = handler.obtainMessage();
                    if (j != p) {
                        msg.what = 0;
                        msg.arg1 = p;
                        handler.sendMessage(msg);
                    }
                    j = p;
                }
            }
        }, apkType).execute(downloadUrl);
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
 
    }
 
    /**
     * 下载进度推送
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    notif.contentView.setTextViewText(R.id.content_view_per,
                            msg.arg1 + "%");
                    manager.notify(0, notif);
                    break;
                case 1:
                    notif.contentView
                            .setTextViewText(R.id.content_view_per, "下载完成");
                    manager.notify(0, notif);
                    j = 100;
                    manager.cancelAll();
                    stopSelf();
                    break;
                default:
                    break;
            }
        }
 
    };
 
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
}