Skip to content
This repository was archived by the owner on Nov 10, 2017. It is now read-only.

Commit 415e3d8

Browse files
committed
Fix #22 and refactor service binding code
1 parent deb7bd5 commit 415e3d8

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/main/java/pt/lighthouselabs/obd/reader/activity/MainActivity.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
100100
};
101101
private final Runnable mQueueCommands = new Runnable() {
102102
public void run() {
103-
if (service.isRunning())
103+
if (service!=null && service.isRunning()) {
104104
queueCommands();
105+
}
105106
// run again in 2s
106107
new Handler().postDelayed(mQueueCommands, 2000);
107108
}
@@ -125,15 +126,21 @@ public void run() {
125126

126127
private AbstractGatewayService service;
127128
private ServiceConnection serviceConn = new ServiceConnection() {
129+
@Override
128130
public void onServiceConnected(ComponentName className, IBinder binder) {
129131
Log.d(TAG, className.toString() + " service is bound");
130132
isServiceBound = true;
131133
service = ((AbstractGatewayService.AbstractGatewayServiceBinder)binder).getService();
132134
service.setContext(MainActivity.this);
133135
Log.d(TAG, "Starting the live data");
136+
service.startService();
134137

135138
}
136139

140+
// This method is *only* called when the connection to the service is lost unexpectedly
141+
// and *not* when the client unbinds (http://developer.android.com/guide/components/bound-services.html)
142+
// So the isServiceBound attribute should also be set to false when we unbind from the service.
143+
@Override
137144
public void onServiceDisconnected(ComponentName className) {
138145
Log.d(TAG, className.toString() + " service is unbound");
139146
isServiceBound = false;
@@ -206,20 +213,17 @@ public void onCreate(Bundle savedInstanceState) {
206213
protected void onStart() {
207214
super.onStart();
208215
Log.d(TAG, "Entered onStart...");
209-
// bind service
210-
if (!isServiceBound) {
211-
doBindService();
212-
}
216+
213217
}
214218

215219
@Override
216220
protected void onDestroy() {
217221
super.onDestroy();
218222

219223
releaseWakeLockIfHeld();
220-
if (isServiceBound)
224+
if (isServiceBound) {
221225
doUnbindService();
222-
;
226+
}
223227
}
224228

225229
@Override
@@ -291,11 +295,8 @@ private void getTroubleCodes() {
291295

292296
private void startLiveData() {
293297
Log.d(TAG, "Starting live data..");
294-
Log.d(TAG, "Service is bound? " + isServiceBound + ", and running? " + service.isRunning());
295-
if (isServiceBound && !service.isRunning()) {
296-
Log.d(TAG, "Service is not running. Going to start it..");
297-
service.startService();
298-
}
298+
299+
doBindService();
299300

300301
// start command execution
301302
new Handler().post(mQueueCommands);
@@ -306,8 +307,9 @@ private void startLiveData() {
306307

307308
private void stopLiveData() {
308309
Log.d(TAG, "Stopping live data..");
309-
if (isServiceBound)
310-
doUnbindService();
310+
311+
doUnbindService();
312+
311313
releaseWakeLockIfHeld();
312314
}
313315

@@ -418,6 +420,7 @@ private void doUnbindService() {
418420
}
419421
Log.d(TAG, "Unbinding OBD service..");
420422
unbindService(serviceConn);
423+
isServiceBound = false;
421424
}
422425
}
423426

0 commit comments

Comments
 (0)