瀏覽代碼

Add menu item to start/stop service

Fix db leak
master
Chris Smith 14 年之前
父節點
當前提交
2acf35a488

二進制
code/ContextAnalyser/dist/ContextAnalyser.apk 查看文件


+ 20
- 3
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java 查看文件

@@ -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
 }

+ 4
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java 查看文件

@@ -266,6 +266,10 @@ public class DataHelper {
266 266
         }
267 267
     }
268 268
 
269
+    public void close() {
270
+        db.close();
271
+    }
272
+
269 273
     private static class OpenHelper extends SQLiteOpenHelper {
270 274
 
271 275
         public OpenHelper(final Context context) {

+ 31
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/IntroActivity.java 查看文件

@@ -24,7 +24,10 @@ package uk.co.md87.android.contextanalyser;
24 24
 
25 25
 import android.app.Activity;
26 26
 import android.content.Intent;
27
+import android.content.SharedPreferences;
27 28
 import android.os.Bundle;
29
+import android.view.Menu;
30
+import android.view.MenuItem;
28 31
 import android.webkit.WebView;
29 32
 
30 33
 /**
@@ -43,7 +46,34 @@ public class IntroActivity extends Activity {
43 46
         final WebView webview = (WebView) findViewById(R.id.webview);
44 47
         webview.loadUrl("http://chris.smith.name/android/contextanalyser/");
45 48
 
46
-        startService(new Intent(this, ContextAnalyserService.class));
49
+        SharedPreferences prefs = getSharedPreferences("contextanalyser", MODE_WORLD_READABLE);
50
+
51
+        if (prefs.getBoolean("run", true)) {
52
+            startService(new Intent(this, ContextAnalyserService.class));
53
+        }
54
+    }
55
+
56
+    @Override
57
+    public boolean onPrepareOptionsMenu(Menu menu) {
58
+        SharedPreferences prefs = getSharedPreferences("contextanalyser", MODE_WORLD_READABLE);
59
+        menu.clear();
60
+        menu.add(0, 0, 0, prefs.getBoolean("run", true) ? "Disable service" : "Enable service");
61
+        return super.onPrepareOptionsMenu(menu);
62
+    }
63
+
64
+    @Override
65
+    public boolean onOptionsItemSelected(MenuItem item) {
66
+        SharedPreferences prefs = getSharedPreferences("contextanalyser", MODE_WORLD_READABLE);
67
+        boolean old = prefs.getBoolean("run", true);
68
+        prefs.edit().putBoolean("run", !old).commit();
69
+
70
+        if (!old) {
71
+            startService(new Intent(this, ContextAnalyserService.class));
72
+        }
73
+        
74
+        return super.onOptionsItemSelected(item);
47 75
     }
48 76
 
77
+
78
+
49 79
 }

Loading…
取消
儲存