From 449733dec41c039ec7e6f23d81f93b63c0bfbf9e Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 26 Jul 2014 22:03:11 -0700 Subject: [PATCH] Determine the proper status of the service when starting the config activity --- src/org/cgutman/usbip/config/UsbIpConfig.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/org/cgutman/usbip/config/UsbIpConfig.java b/src/org/cgutman/usbip/config/UsbIpConfig.java index a0538c6..a01475b 100644 --- a/src/org/cgutman/usbip/config/UsbIpConfig.java +++ b/src/org/cgutman/usbip/config/UsbIpConfig.java @@ -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; @@ -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) { @@ -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); @@ -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() {