admin
7 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
127
128
129
130
131
package com.weikou.beibeivideo.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.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.AdTag;
import com.weikou.beibeivideo.util.downutil.DownLoadApks;
import com.weikou.beibeivideo.util.downutil.DownLoadApks.IProgress;
 
import java.io.File;
 
import org.greenrobot.eventbus.EventBus;
 
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 0;
        }
        Bundle bundle = (Bundle) intent.getExtras();
        downloadUrl = bundle.getString("downloadurl", "");
        if (downloadUrl.contains("memezhibo")) {
            apkType = "com.memezhibo.android";
        } else {
            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.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) {
 
            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");
                    if (a.listFiles().length == 2) {
                        AdTag tag=new AdTag();
                        EventBus.getDefault().post(tag);
                    }
                    manager.cancelAll();
                    stopSelf();
                    break;
                default:
                    break;
            }
        }
 
    };
 
    @Override
    public void onStart(Intent intent, int startId) {
 
        super.onStart(intent, startId);
    }
 
    @Override
    public void onDestroy() {
 
        super.onDestroy();
    }
 
}