93 可读性
 将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

In android: I want to check the service running or not if already running change the UI colors when i launch
在 android: 我想检查服务运行,否则,如果已经经运行了改变UI颜色,我启动

76 0

In my android application have check the whether the service run or not if running i want to change the UI color.i use the following code

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

but it showing the error that..

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)

my UI change code>

 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));
 }
}

any one help me...

时间:原作者:2个回答

132 0

Follow this code :

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;
 }

Call this method in your activity like below :

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