|
@@ -24,6 +24,7 @@ package uk.co.md87.android.contextanalyser;
|
24
|
24
|
|
25
|
25
|
import android.app.Service;
|
26
|
26
|
import android.content.Intent;
|
|
27
|
+import android.content.SharedPreferences;
|
27
|
28
|
import android.location.Address;
|
28
|
29
|
import android.location.Geocoder;
|
29
|
30
|
import android.location.Location;
|
|
@@ -61,7 +62,8 @@ import uk.co.md87.android.contextanalyser.rpc.ContextAnalyserBinder;
|
61
|
62
|
*
|
62
|
63
|
* @author chris
|
63
|
64
|
*/
|
64
|
|
-public class ContextAnalyserService extends Service {
|
|
65
|
+public class ContextAnalyserService extends Service
|
|
66
|
+ implements SharedPreferences.OnSharedPreferenceChangeListener {
|
65
|
67
|
|
66
|
68
|
public static final String ACTIVITY_CHANGED_INTENT
|
67
|
69
|
= "uk.co.md87.android.contextanalyser.ACTIVITY_CHANGED";
|
|
@@ -121,8 +123,8 @@ public class ContextAnalyserService extends Service {
|
121
|
123
|
private Handler handler = new Handler();
|
122
|
124
|
|
123
|
125
|
@Override
|
124
|
|
- public void onCreate() {
|
125
|
|
- super.onCreate();
|
|
126
|
+ public void onStart(Intent intent, int startId) {
|
|
127
|
+ super.onStart(intent, startId);
|
126
|
128
|
|
127
|
129
|
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
|
128
|
130
|
|
|
@@ -139,6 +141,11 @@ public class ContextAnalyserService extends Service {
|
139
|
141
|
handler.postDelayed(scheduleRunnable, POLLING_DELAY);
|
140
|
142
|
|
141
|
143
|
FlurryAgent.onStartSession(this, "MKB8YES3C6CFB86PXYXK");
|
|
144
|
+
|
|
145
|
+ Log.i("ContextAnalyser", "Starting...");
|
|
146
|
+
|
|
147
|
+ SharedPreferences prefs = getSharedPreferences("contextanalyser", MODE_WORLD_READABLE);
|
|
148
|
+ prefs.registerOnSharedPreferenceChangeListener(this);
|
142
|
149
|
}
|
143
|
150
|
|
144
|
151
|
public void poll() {
|
|
@@ -346,6 +353,8 @@ public class ContextAnalyserService extends Service {
|
346
|
353
|
public void onDestroy() {
|
347
|
354
|
super.onDestroy();
|
348
|
355
|
|
|
356
|
+ dataHelper.close();
|
|
357
|
+
|
349
|
358
|
handler.removeCallbacks(scheduleRunnable);
|
350
|
359
|
FlurryAgent.onEndSession(this);
|
351
|
360
|
}
|
|
@@ -355,4 +364,12 @@ public class ContextAnalyserService extends Service {
|
355
|
364
|
return binder;
|
356
|
365
|
}
|
357
|
366
|
|
|
367
|
+ public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
|
368
|
+ if (!prefs.getBoolean("run", true)) {
|
|
369
|
+ Log.i("ContextAnalyser", "Stopping...");
|
|
370
|
+ stopSelf();
|
|
371
|
+ prefs.unregisterOnSharedPreferenceChangeListener(this);
|
|
372
|
+ }
|
|
373
|
+ }
|
|
374
|
+
|
358
|
375
|
}
|