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

Commit

Permalink
Fix #22 and refactor service binding code
Browse files Browse the repository at this point in the history
  • Loading branch information
spapas committed Jul 28, 2014
1 parent deb7bd5 commit 415e3d8
Showing 1 changed file with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
};
private final Runnable mQueueCommands = new Runnable() {
public void run() {
if (service.isRunning())
if (service!=null && service.isRunning()) {
queueCommands();
}
// run again in 2s
new Handler().postDelayed(mQueueCommands, 2000);
}
Expand All @@ -125,15 +126,21 @@ public void run() {

private AbstractGatewayService service;
private ServiceConnection serviceConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.d(TAG, className.toString() + " service is bound");
isServiceBound = true;
service = ((AbstractGatewayService.AbstractGatewayServiceBinder)binder).getService();
service.setContext(MainActivity.this);
Log.d(TAG, "Starting the live data");
service.startService();

}

// This method is *only* called when the connection to the service is lost unexpectedly
// and *not* when the client unbinds (http://developer.android.com/guide/components/bound-services.html)
// So the isServiceBound attribute should also be set to false when we unbind from the service.
@Override
public void onServiceDisconnected(ComponentName className) {
Log.d(TAG, className.toString() + " service is unbound");
isServiceBound = false;
Expand Down Expand Up @@ -206,20 +213,17 @@ public void onCreate(Bundle savedInstanceState) {
protected void onStart() {
super.onStart();
Log.d(TAG, "Entered onStart...");
// bind service
if (!isServiceBound) {
doBindService();
}

}

@Override
protected void onDestroy() {
super.onDestroy();

releaseWakeLockIfHeld();
if (isServiceBound)
if (isServiceBound) {
doUnbindService();
;
}
}

@Override
Expand Down Expand Up @@ -291,11 +295,8 @@ private void getTroubleCodes() {

private void startLiveData() {
Log.d(TAG, "Starting live data..");
Log.d(TAG, "Service is bound? " + isServiceBound + ", and running? " + service.isRunning());
if (isServiceBound && !service.isRunning()) {
Log.d(TAG, "Service is not running. Going to start it..");
service.startService();
}

doBindService();

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

private void stopLiveData() {
Log.d(TAG, "Stopping live data..");
if (isServiceBound)
doUnbindService();

doUnbindService();

releaseWakeLockIfHeld();
}

Expand Down Expand Up @@ -418,6 +420,7 @@ private void doUnbindService() {
}
Log.d(TAG, "Unbinding OBD service..");
unbindService(serviceConn);
isServiceBound = false;
}
}

Expand Down

0 comments on commit 415e3d8

Please sign in to comment.