Pārlūkot izejas kodu

Add menu item to start/stop service

Fix db leak
master
Chris Smith 14 gadus atpakaļ
vecāks
revīzija
2acf35a488

Binārs
code/ContextAnalyser/dist/ContextAnalyser.apk Parādīt failu


+ 20
- 3
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java Parādīt failu

24
 
24
 
25
 import android.app.Service;
25
 import android.app.Service;
26
 import android.content.Intent;
26
 import android.content.Intent;
27
+import android.content.SharedPreferences;
27
 import android.location.Address;
28
 import android.location.Address;
28
 import android.location.Geocoder;
29
 import android.location.Geocoder;
29
 import android.location.Location;
30
 import android.location.Location;
61
  *
62
  *
62
  * @author chris
63
  * @author chris
63
  */
64
  */
64
-public class ContextAnalyserService extends Service {
65
+public class ContextAnalyserService extends Service
66
+        implements SharedPreferences.OnSharedPreferenceChangeListener {
65
 
67
 
66
     public static final String ACTIVITY_CHANGED_INTENT
68
     public static final String ACTIVITY_CHANGED_INTENT
67
             = "uk.co.md87.android.contextanalyser.ACTIVITY_CHANGED";
69
             = "uk.co.md87.android.contextanalyser.ACTIVITY_CHANGED";
121
     private Handler handler = new Handler();
123
     private Handler handler = new Handler();
122
 
124
 
123
     @Override
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
         Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
129
         Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
128
 
130
 
139
         handler.postDelayed(scheduleRunnable, POLLING_DELAY);
141
         handler.postDelayed(scheduleRunnable, POLLING_DELAY);
140
 
142
 
141
         FlurryAgent.onStartSession(this, "MKB8YES3C6CFB86PXYXK");
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
     public void poll() {
151
     public void poll() {
346
     public void onDestroy() {
353
     public void onDestroy() {
347
         super.onDestroy();
354
         super.onDestroy();
348
 
355
 
356
+        dataHelper.close();
357
+
349
         handler.removeCallbacks(scheduleRunnable);
358
         handler.removeCallbacks(scheduleRunnable);
350
         FlurryAgent.onEndSession(this);
359
         FlurryAgent.onEndSession(this);
351
     }
360
     }
355
         return binder;
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 Parādīt failu

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

+ 31
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/IntroActivity.java Parādīt failu

24
 
24
 
25
 import android.app.Activity;
25
 import android.app.Activity;
26
 import android.content.Intent;
26
 import android.content.Intent;
27
+import android.content.SharedPreferences;
27
 import android.os.Bundle;
28
 import android.os.Bundle;
29
+import android.view.Menu;
30
+import android.view.MenuItem;
28
 import android.webkit.WebView;
31
 import android.webkit.WebView;
29
 
32
 
30
 /**
33
 /**
43
         final WebView webview = (WebView) findViewById(R.id.webview);
46
         final WebView webview = (WebView) findViewById(R.id.webview);
44
         webview.loadUrl("http://chris.smith.name/android/contextanalyser/");
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
 }

Notiek ielāde…
Atcelt
Saglabāt