admin
2021-05-11 e5ca87e89fef96c827ec37d1d91082f626cbb17d
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
118
119
120
121
122
123
124
125
126
package com.tejia.lijin.app.service;
 
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.tejia.lijin.app.R;
import com.tejia.lijin.app.util.downutil.DownLoadApks;
 
import java.io.File;
 
 
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;
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            return Service.START_STICKY_COMPATIBILITY;
        }
        Bundle bundle = intent.getExtras();
        downloadUrl = bundle.getString("downloadurl", "");
        if (downloadUrl.contains("memezhibo")) {
            apkType = "com.memezhibo.android";
        } else {
            apkType = "";
        }
        new DownLoadApks(this, new DownLoadApks.IProgress() {
 
            @Override
            public void getProgress(int p) {
                // TODO Auto-generated method
                // 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.icon = R.drawable.ic_launcher;
                        notif.tickerText = "插件下载";
                        // 通知栏显示所用到的布局文件
                        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) {
            // TODO Auto-generated method stub
            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;
                    File a = getExternalFilesDir("pptv");
                    manager.cancelAll();
                    stopSelf();
                    break;
                default:
                    break;
            }
        }
 
    };
 
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    }
 
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
 
}