I was trying to override the toString() function of a service. I have a service and I am using another app to access the toString(). However the output is not as expected, I suspect that the overriding of toString() has failed ! Can anyone help ? Thanks
///////////////toString Functi开发者_如何学Con in the service !///////////////
public String toString() {
return "Hello World";
}
//////////how I am calling the overridden function !////////////////////
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);
for (int i=0; i<rs.size(); i++) {
ActivityManager.RunningServiceInfo rsi = rs.get(i);
Log.i("Hello",rsi.getClass().toString());
Log.i("Service", "Process " + rsi.process + " with component " +
rsi.service.getClassName());
}
You expect to have rsi.getClass().toString()
return "Hello World" right?
It doesn't work as expected above, because you call another toString
method in another object. You call this function: http://developer.android.com/reference/java/lang/Class.html#toString()
Problem here is, that RunningServiceInfo
only tells you about what Service is running. It doesn't actually give you the object of your service.
Now how can you get it correct? Depends on what you want to do exactly.
But the nicest way to access your service out of another application is to bind to it. If your Service is in the same app, you can follow this tutorial: http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/
How to do that out of another application:
how to bind a remote service that is installed on my android device?
精彩评论