Skip to content

Commit

Permalink
Determine the proper status of the service when starting the config a…
Browse files Browse the repository at this point in the history
…ctivity
  • Loading branch information
cgutman committed Jul 27, 2014
1 parent e6dcab0 commit 449733d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/org/cgutman/usbip/config/UsbIpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.cgutman.usbipserverforandroid.R;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
Expand All @@ -15,7 +18,7 @@ public class UsbIpConfig extends Activity {
private Button serviceButton;
private TextView serviceStatus;

private boolean running = false;
private boolean running;

private void updateStatus() {
if (running) {
Expand All @@ -28,6 +31,17 @@ private void updateStatus() {
}
}

// Elegant Stack Overflow solution to querying running services
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -36,6 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
serviceButton = (Button) findViewById(R.id.serviceButton);
serviceStatus = (TextView) findViewById(R.id.serviceStatus);

running = isMyServiceRunning(UsbIpService.class);

updateStatus();

serviceButton.setOnClickListener(new OnClickListener() {
Expand Down

0 comments on commit 449733d

Please sign in to comment.