5
5
import android .content .BroadcastReceiver ;
6
6
import android .content .Context ;
7
7
import android .content .Intent ;
8
+ import android .content .pm .PackageInfo ;
9
+ import android .content .pm .PackageManager ;
8
10
import android .net .ConnectivityManager ;
9
11
import android .os .Build ;
10
12
import android .os .Handler ;
35
37
*/
36
38
public class UpdateCheckService extends Service {
37
39
38
- // private static final long CHECK_UPDATE_INTERVAL = 6 * 60 * 60 * 1000; // 6 hrs interval
39
- private static final long CHECK_UPDATE_INTERVAL = 20 * 1000 ; // 20 sec interval
40
+ private static final long CHECK_UPDATE_INTERVAL = 6 * 60 * 60 * 1000 ; // 6 hrs interval
41
+ // private static final long CHECK_UPDATE_INTERVAL = 20 * 1000; // 20 sec interval
40
42
private static final int SERVER_TIMEOUT_LIMIT = 10 * 1000 ; // 10 sec
41
43
private static Timer mTimer ;
42
44
Handler mHandler = new Handler ();
@@ -64,8 +66,6 @@ public void onCreate() {
64
66
private void checkForUpdate () {
65
67
66
68
Log .d ("UpdateServiceAnyAudio" , " UpdateCheck...." );
67
-
68
-
69
69
StringRequest updateCheckReq = new StringRequest (
70
70
Request .Method .GET ,
71
71
url ,
@@ -104,17 +104,35 @@ public boolean isForeground(String myPackage) {
104
104
105
105
ActivityManager mActivityManager = (ActivityManager ) this .getSystemService (ACTIVITY_SERVICE );
106
106
String mpackageName = "" ;
107
+
107
108
if (Build .VERSION .SDK_INT > 20 ) {
109
+
108
110
mpackageName = String .valueOf (mActivityManager .getRunningAppProcesses ().get (0 ).processName );
111
+
109
112
} else {
113
+
110
114
mpackageName = String .valueOf (mActivityManager .getRunningTasks (1 ).get (0 ).topActivity .getClassName ());
115
+
111
116
}
117
+
112
118
L .m ("UpdateService" , "found " + mpackageName );
113
119
return mpackageName .equals (myPackage );
120
+
114
121
}
115
122
116
123
private int getCurrentAppVersionCode () {
117
- return SharedPrefrenceUtils .getInstance (getApplicationContext ()).getCurrentVersionCode ();
124
+
125
+ try {
126
+
127
+ PackageInfo _info = this .getPackageManager ().getPackageInfo (this .getPackageName (), 0 );
128
+ return _info .versionCode ;
129
+
130
+ } catch (PackageManager .NameNotFoundException e ) {
131
+
132
+ e .printStackTrace ();
133
+ return -1 ;
134
+
135
+ }
118
136
}
119
137
120
138
public void handleNewUpdateResponse (String response ) {
@@ -130,12 +148,12 @@ public void handleNewUpdateResponse(String response) {
130
148
* */
131
149
132
150
try {
133
- JSONObject updateResp = new JSONObject (response );
134
151
152
+ JSONObject updateResp = new JSONObject (response );
135
153
double newVersion = updateResp .getDouble ("version" );
136
154
String updateDescription = updateResp .getString ("newInThisUpdate" );
137
155
String downloadUrl = updateResp .getString ("appDownloadUrl" );
138
- Log .d ("UpdateServiceTest" ," new Version " + newVersion + " old version " + getCurrentAppVersionCode ()+ " update Des " + updateDescription );
156
+ Log .d ("UpdateServiceTest" , " new Version " + newVersion + " old version " + getCurrentAppVersionCode () + " update Des " + updateDescription );
139
157
140
158
if (newVersion > getCurrentAppVersionCode ()) {
141
159
// write update to shared pref..
@@ -149,9 +167,5 @@ public void handleNewUpdateResponse(String response) {
149
167
} catch (JSONException e ) {
150
168
e .printStackTrace ();
151
169
}
152
-
153
-
154
170
}
155
-
156
-
157
171
}
0 commit comments