如何重写android服务中的toString()方法?

发布于 2024-10-01 21:15:41 字数 741 浏览 2 评论 0原文

我试图重写服务的 toString() 函数。我有一项服务,并且正在使用另一个应用程序来访问 toString()。然而输出并不符合预期,我怀疑 toString() 的重写失败了!有人可以帮忙吗?谢谢

///////////////toString Function 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());
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-10-08 21:15:41

您希望 rsi.getClass().toString() 返回“Hello World”,对吧?
它无法按上面的预期工作,因为您在另一个对象中调用了另一个 toString 方法。您调用此函数: http://developer.android.com /reference/java/lang/Class.html#toString()

这里的问题是,RunningServiceInfo 只告诉您正在运行的服务。它实际上并没有给你提供服务的对象。

现在你怎样才能得到正确的结果呢?取决于你具体想做什么。

但是从另一个应用程序访问您的服务的最好方法是绑定到它。
如果您的服务位于同一个应用程序中,您可以按照本教程操作:
http://www.androidcompetencycenter.com /2009/01/basics-of-android-part-iii-android-services/

如何在另一个应用程序中执行此操作:
如何绑定安装在我的 Android 设备上的远程服务?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文