gpt4 book ai didi

Android 服务 - 似乎无法找到一个例子

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:57 25 4
gpt4 key购买 nike

我已经在网上搜索过了,我似乎无法弄清楚如何让 Android 服务真正做任何事情。我找到了很多关于如何创建基本服务的示例,但似乎没有一个示例显示如何从 Activity 中调用服务,并让服务执行某些操作。

例如,我希望运行一个服务,该服务将在收到请求时向服务器发送 TCP 文本消息。我可以创建服务并让它运行,但我到底如何让 Activity 进行调用,将字符串传递给服务中将发送 TCP 消息的方法?

这看起来应该比较容易,但我就是想不出来,也找不到一个例子来说明如何做。也许我不明白应该使用什么服务?我绝对希望它运行很长一段时间,不需要 gui,并且“服务”请求发送 TCP 消息....哼...

最佳答案

I can make the service, and have it run, but how the heck do I have the Activity make the call that passes a string to the method in the service that will send the TCP message?

使用本地绑定(bind)模式。在 this sample project ,一个 Activity 绑定(bind)到一个服务,以便根据位置变化获取该服务(天气预报)检索的一些数据。在 this sample project ,一个 Activity 绑定(bind)到一个服务以注册一个监听器对象,以便在用户的 identi.ca 时间线发生变化时得到通知。

或者,使用 IntentServicestartService() 发送要由 IntentService 处理的命令。在 this sample project ,我实现了一个 IntentService 来执行调用者提供的 BeanShell 脚本。在 this sample project ,我实现了一个将 BeanShell 脚本发送到 IntentService 的 Activity 。这对示例旨在演示跨应用程序使用此技术,但该方法在单个应用程序中也能正常工作。

I definitely want it running for a long period of time

No you don't .

关于Android 服务 - 似乎无法找到一个例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com