在 onCreate 上绑定后立即从 android 服务调用方法

发布于 2024-09-17 06:00:45 字数 756 浏览 9 评论 0原文

我很确定 Android 服务将是我的终结。过去几天后,我的头发几乎没有了……

无论如何,我离题了。

起初,我很难让服务绑定到单击按钮上,昨天从这里的帮助中解决了这个问题。现在我实际上希望该服务绑定在活动的 onCreate() 方法中。该部分没有问题,按预期工作。我的服务实际上是一个套接字服务,用于将 TCP 套接字连接到我编写的套接字服务器。如果我将对绑定服务 mBoundService.StartSocketServer() 的方法的调用放在按钮单击内,宾果游戏,效果很好。但我需要在活动加载时立即启动它,因此直接在活动的 onCreate() 方法中的 bindService() 调用下启动。当我在 onCreate() 中调用启动套接字时,我得到了强制关闭。

此方法 (StartSocketServer()) 生成一个新线程,然后打开该线程上到我的远程计算机的套接字。我猜测问题在于活动完全加载之前新线程的生成......不确定。

LogCat 在这里相当神秘。它确实说了一些有关线程附加失败的信息,然后显示一个未捕获的处理程序异常,其中包含“Caused by: java.lang.NullPointerException”。

再说一次,如果我将此调用放在按钮单击内的方法,我就在做生意,如果它在 onCreate() 中,它就会失败。活动内部是否有某种方法(假设我的假设是正确的,它需要完全在生成新线程之前加载)以在加载后调用 StartSocketServer():html 中的 ala body.onLoad() 吗?

请帮我保存其余的头发:)

TIA

I'm pretty sure that android services are going to be the end of me. I have almost no hair left after the last few days....

...anyway, I digress.

At first I was having a heck of a time getting the service to bind on an onclick of a button, got that straightened out from help here yesterday. Now I actually want the service to bind in the onCreate() method of the activity. That part is no problem, works as intended. My service is actually a socket service to connect TCP sockets to a socket server that I wrote. If I put the call to the method from the bound service mBoundService.StartSocketServer() inside a button click, bingo, works great. But I need this to fire up immediately when the activity loads, so directly under my bindService() call within my onCreate() method of the activity. When I place the call to start the socket inside my onCreate() I get a force-close.

This method (StartSocketServer()) spawns a new thread then opens the socket on that thread to my remote machine. I'm guessing that the problem lies with the new thread generation before the activity fully loads...not sure.

LogCat is fairly cryptic here. It does say something about thread attach failed, then shows an uncaught handler exception that has "Caused by: java.lang.NullPointerException" within it.

Again, if I put this call to the method inside a button click, I"m in business, if it's in the onCreate() it fails. Is there some way inside an activity (presuming that my assumption is correct that it needs to fully load before spawning a new thread) to call the StartSocketServer() after it's loaded: ala body.onLoad() in html?

please help me save the rest of my hair :)

TIA

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

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

发布评论

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

评论(1

烦人精 2024-09-24 06:00:45

正如我在您的之前的问题中所写:

FWIW,bindService() 是异步的。在 onServiceConnected() 之前,您不会有绑定连接
调用您的 ServiceConnection。

bindService() 调用中,您提供一个 ServiceConnection 对象。当服务可供使用时,将使用 onServiceConnected() 调用该对象。除此之外,这可能是您填充mService的地方。将您的调用放在 onServiceConnected() 中的 StartSocketServer() 中。

As I wrote in your earlier question:

FWIW, bindService() is asynchronous. You will not have a bound connection until onServiceConnected() is
called on your ServiceConnection.

In your bindService() call, you are supplying a ServiceConnection object. That object will be called with onServiceConnected() when the service is ready for use. Among other things, that is probably where you are populating mService. Put your call to StartSocketServer() in onServiceConnected().

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