4
4
import org .cgutman .usbipserverforandroid .R ;
5
5
6
6
import android .app .Activity ;
7
+ import android .app .ActivityManager ;
8
+ import android .app .ActivityManager .RunningServiceInfo ;
9
+ import android .content .Context ;
7
10
import android .content .Intent ;
8
11
import android .os .Bundle ;
9
12
import android .view .View ;
@@ -15,7 +18,7 @@ public class UsbIpConfig extends Activity {
15
18
private Button serviceButton ;
16
19
private TextView serviceStatus ;
17
20
18
- private boolean running = false ;
21
+ private boolean running ;
19
22
20
23
private void updateStatus () {
21
24
if (running ) {
@@ -28,6 +31,17 @@ private void updateStatus() {
28
31
}
29
32
}
30
33
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
+
31
45
@ Override
32
46
protected void onCreate (Bundle savedInstanceState ) {
33
47
super .onCreate (savedInstanceState );
@@ -36,6 +50,8 @@ protected void onCreate(Bundle savedInstanceState) {
36
50
serviceButton = (Button ) findViewById (R .id .serviceButton );
37
51
serviceStatus = (TextView ) findViewById (R .id .serviceStatus );
38
52
53
+ running = isMyServiceRunning (UsbIpService .class );
54
+
39
55
updateStatus ();
40
56
41
57
serviceButton .setOnClickListener (new OnClickListener () {
0 commit comments