multithreading - 在 android: 我想检查服务运行,否则,如果已经经运行了改变UI颜色,我启动

  显示原文与译文双语对照的内容
76 0

在我的android应用程序中检查服务是否运行,如果运行我想更改UI颜色。我使用下面的代码

 if(true==util.isMyServiceRunning(getApplicationContext())){
 makeUIChangesforTurnOn(true);
 }

但它显示出了错误。

09-09 00:53:26.810: E/AndroidRuntime(1106): FATAL EXCEPTION: main 09-09 00:53:26.810: 
E/AndroidRuntime(1106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.param.calltracker/com.param.calltracker.MainActivity}:java.lang.NullPointerException
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.os.Looper.loop(Looper.java:130)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-09 00:53:26.810: E/AndroidRuntime(1106): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 00:53:26.810: E/AndroidRuntime(1106): at java.lang.reflect.Method.invoke(Method.java:507)
09-09 00:53:26.810: E/AndroidRuntime(1106): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-09 00:53:26.810: E/AndroidRuntime(1106): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-09 00:53:26.810: E/AndroidRuntime(1106): at dalvik.system.NativeStart.main(Native Method)
09-09 00:53:26.810: E/AndroidRuntime(1106): Caused by: java.lang.NullPointerException
09-09 00:53:26.810: E/AndroidRuntime(1106): at com.param.calltracker.MainActivity.makeUIChangesforTurnOn(MainActivity.java:214)
09-09 00:53:26.810: E/AndroidRuntime(1106): at com.param.calltracker.MainActivity.onCreate(MainActivity.java:79)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 00:53:26.810: E/AndroidRuntime(1106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

我的用户界面更改代码>

 public void makeUIChangesforTurnOn(Boolean on){
 if(on){
 trun_on_off_img.setImageResource(R.drawable.green_signal);
 buttonToggleDetect.setBackgroundResource(R.drawable.orenge_button);
 buttonToggleDetect.setText("Turn off");
 textViewDetectState.setText("Monitoring Calls");
 textViewDetectState.setTextColor(getResources().getColor(R.color.green));
 }else{
 trun_on_off_img.setImageResource(R.drawable.yellow_signal);
 buttonToggleDetect.setBackgroundResource(R.drawable.green_button);
 buttonToggleDetect.setText("Turn on");
 textViewDetectState.setText("Not Monitoring Calls");
 textViewDetectState.setTextColor(getResources().getColor(R.color.orenge));
 }
}

任何一个帮助我。

时间:原作者:2个回答

132 0

遵循以下代码:

private boolean isMyServiceRunning() {
 ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
 for (RunningServiceInfo service : manager
. getRunningServices(Integer.MAX_VALUE)) {
 if (YourService.class.getName().equals(
 service.service.getClassName())) {
 return true;
 }
 }
 return false;
 }

在你的Activity 中调用这里方法,如下所示:

 makeChangesInUI(isMyServiceRunning())
原作者:
...