Skip to content

Commit 449733d

Browse files
committed
Determine the proper status of the service when starting the config activity
1 parent e6dcab0 commit 449733d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/org/cgutman/usbip/config/UsbIpConfig.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.cgutman.usbipserverforandroid.R;
55

66
import android.app.Activity;
7+
import android.app.ActivityManager;
8+
import android.app.ActivityManager.RunningServiceInfo;
9+
import android.content.Context;
710
import android.content.Intent;
811
import android.os.Bundle;
912
import android.view.View;
@@ -15,7 +18,7 @@ public class UsbIpConfig extends Activity {
1518
private Button serviceButton;
1619
private TextView serviceStatus;
1720

18-
private boolean running = false;
21+
private boolean running;
1922

2023
private void updateStatus() {
2124
if (running) {
@@ -28,6 +31,17 @@ private void updateStatus() {
2831
}
2932
}
3033

34+
// Elegant Stack Overflow solution to querying running services
35+
private boolean isMyServiceRunning(Class<?> serviceClass) {
36+
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
37+
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
38+
if (serviceClass.getName().equals(service.service.getClassName())) {
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
3145
@Override
3246
protected void onCreate(Bundle savedInstanceState) {
3347
super.onCreate(savedInstanceState);
@@ -36,6 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
3650
serviceButton = (Button) findViewById(R.id.serviceButton);
3751
serviceStatus = (TextView) findViewById(R.id.serviceStatus);
3852

53+
running = isMyServiceRunning(UsbIpService.class);
54+
3955
updateStatus();
4056

4157
serviceButton.setOnClickListener(new OnClickListener() {

0 commit comments

Comments
 (0)