1
+ package com .sd .one .utils ;
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 .net .Uri ;
10
+ import android .os .Environment ;
11
+ import android .os .IBinder ;
12
+ import android .text .TextUtils ;
13
+
14
+ import com .sd .core .utils .NLog ;
15
+
16
+ import java .io .File ;
17
+
18
+ /**
19
+ * [版本更新服务类]
20
+ *
21
+ * @author devin.hu
22
+ * @version 1.0
23
+ * @date 2014-2-25
24
+ *
25
+ **/
26
+ public class VersionUpdateService extends Service {
27
+
28
+ private static final String TAG = VersionUpdateService .class .getSimpleName ();
29
+
30
+ private DownloadManager mDownloadManager ;
31
+ private BroadcastReceiver mReceiver ;
32
+ private String mDesc ;
33
+ private String mFileName ;
34
+ private String mUrl ;
35
+
36
+
37
+ @ Override
38
+ public IBinder onBind (Intent intent ) {
39
+ return null ;
40
+ }
41
+
42
+ @ Override
43
+ public int onStartCommand (Intent intent , int flags , int startId ) {
44
+ mDesc = intent .getStringExtra ("desc" );
45
+ mFileName = intent .getStringExtra ("fileName" );
46
+ mUrl = intent .getStringExtra ("url" );
47
+ NLog .e (TAG , "url: " + mUrl );
48
+
49
+ if (TextUtils .isEmpty (mUrl )) {
50
+ stopSelf ();
51
+ }
52
+
53
+ mReceiver = new BroadcastReceiver () {
54
+ @ Override
55
+ public void onReceive (Context context , Intent intent ) {
56
+ intent = new Intent (Intent .ACTION_VIEW );
57
+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
58
+ intent .setDataAndType (Uri .fromFile (new File (Environment .getExternalStorageDirectory () + "/download/" + mFileName )), "application/vnd.android.package-archive" );
59
+ startActivity (intent );
60
+ stopSelf ();
61
+ }
62
+ };
63
+
64
+ registerReceiver (mReceiver , new IntentFilter (DownloadManager .ACTION_DOWNLOAD_COMPLETE ));
65
+ startDownload ();
66
+ return Service .START_STICKY ;
67
+ }
68
+
69
+ @ Override
70
+ public void onDestroy () {
71
+ unregisterReceiver (mReceiver );
72
+ super .onDestroy ();
73
+ }
74
+
75
+ private void startDownload () {
76
+ mDownloadManager = (DownloadManager ) getSystemService (DOWNLOAD_SERVICE );
77
+ DownloadManager .Request request = new DownloadManager .Request (Uri .parse (mUrl ));
78
+ //设置通知栏标题
79
+ request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE );
80
+ request .setTitle ("正在下载" );
81
+ request .setDescription (mDesc );
82
+
83
+ request .setMimeType ("application/vnd.android.package-archive" );
84
+ request .setDestinationInExternalPublicDir (Environment .DIRECTORY_DOWNLOADS , mFileName );
85
+ mDownloadManager .enqueue (request );
86
+ }
87
+
88
+ /**
89
+ * 比较版本,是否有新版本
90
+ **/
91
+ public static boolean hasNewVersion (String webVersion , String localVersion ){
92
+ if (!TextUtils .isEmpty (webVersion )){
93
+ webVersion = (webVersion .replace ("." , "" ));
94
+ }
95
+ if (!TextUtils .isEmpty (localVersion )){
96
+ localVersion = (localVersion .replace ("." , "" ));
97
+ }
98
+
99
+ try {
100
+ if (!TextUtils .isEmpty (webVersion ) && !TextUtils .isEmpty (localVersion )){
101
+ if (Integer .parseInt (webVersion ) > Integer .parseInt (localVersion )){
102
+ return true ;
103
+ }
104
+ }
105
+ }catch (Exception e ){
106
+
107
+ }
108
+ return false ;
109
+ }
110
+ }
0 commit comments