1
+ package com .jingewenku .abrahamcaijin .commonutil ;
2
+
3
+ import android .app .DownloadManager ;
4
+ import android .app .Service ;
5
+ import android .content .BroadcastReceiver ;
6
+ import android .content .Context ;
7
+ import android .content .Intent ;
8
+ import android .content .IntentFilter ;
9
+ import android .database .Cursor ;
10
+ import android .net .Uri ;
11
+ import android .os .IBinder ;
12
+
13
+ /**
14
+ * @Description:主要功能:
15
+ * @Prject: CommonUtilLibrary
16
+ * @Package: com.jingewenku.abrahamcaijin.commonutil
17
+ * @author: AbrahamCaiJin
18
+ * @date: 2017年06月06日 16:10
19
+ * @Copyright: 个人版权所有
20
+ * @Company:
21
+ * @version: 1.0.0
22
+ */
23
+
24
+ public class DownloadService extends Service {
25
+
26
+ private long mTaskId ;
27
+ private DownloadManager mDownloadManager ;
28
+ private String filePath ;
29
+
30
+ public DownloadService () {
31
+ }
32
+
33
+ @ Override
34
+ public void onCreate () {
35
+ super .onCreate ();
36
+ }
37
+
38
+ @ Override
39
+ public int onStartCommand (Intent intent , int flags , int startId ) {
40
+ AppLogMessageMgr .w ("onStartCommand:" + intent .getStringExtra ("fileurl" ));
41
+ String fileurl = intent .getStringExtra ("fileurl" );
42
+ downloadFile (fileurl );
43
+ return super .onStartCommand (intent , flags , startId );
44
+ }
45
+
46
+ @ Override
47
+ public IBinder onBind (Intent intent ) {
48
+ return null ;
49
+ }
50
+
51
+ private void downloadFile (String fileurl ) {
52
+ filePath = "/sdcard/Download/" + fileurl .substring (fileurl .lastIndexOf ("/" ) + 1 );
53
+ DownloadManager .Request request = new DownloadManager .Request (Uri .parse (fileurl ));
54
+ request .setDestinationInExternalPublicDir ("/Download/" , fileurl .substring (fileurl .lastIndexOf ("/" ) + 1 ));
55
+ mDownloadManager = (DownloadManager ) getSystemService (Context .DOWNLOAD_SERVICE );
56
+ mTaskId = mDownloadManager .enqueue (request );
57
+ registerReceiver (mReceiver , new IntentFilter (DownloadManager .ACTION_DOWNLOAD_COMPLETE ));
58
+ }
59
+
60
+ private BroadcastReceiver mReceiver = new BroadcastReceiver () {
61
+ @ Override
62
+ public void onReceive (Context context , Intent intent ) {
63
+ checkDownloadStatus ();
64
+ }
65
+ };
66
+
67
+ private void checkDownloadStatus () {
68
+ DownloadManager .Query query = new DownloadManager .Query ();
69
+ query .setFilterById (mTaskId );
70
+ Cursor c = mDownloadManager .query (query );
71
+ if (c .moveToFirst ()) {
72
+ int status = c .getInt (c .getColumnIndex (DownloadManager .COLUMN_STATUS ));
73
+ switch (status ) {
74
+ /* case DownloadManager.STATUS_PAUSED:
75
+ break;
76
+ case DownloadManager.STATUS_FAILED:
77
+ break;
78
+ case DownloadManager.STATUS_PENDING:
79
+ break;
80
+ case DownloadManager.STATUS_RUNNING:
81
+ break;*/
82
+ case DownloadManager .STATUS_SUCCESSFUL :
83
+ AppApplicationMgr .installApk (this , filePath );
84
+ break ;
85
+ }
86
+ }
87
+
88
+
89
+ }
90
+ }
0 commit comments