java Android Context.bindService 总是返回 false 并且永远不会触发 ServiceConnection 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2914921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:26:32  来源:igfitidea点击:

Android Context.bindService always returns false and ServiceConnection object is never triggered

javaandroidservice

提问by codinguser

I have followed the Local Service exampleprovided by Google, but my Context::bindService(...)always returns false and there is ServiceConnection::onServiceConnectedis also never called.

我遵循了谷歌提供的本地服务示例,但我Context::bindService(...)总是返回 false 并且ServiceConnection::onServiceConnected也从未调用过。

I understand that Context::bindService()returns immediately, but my ServiceConnectionobject is never triggered.

我知道Context::bindService()立即返回,但我的ServiceConnection对象从未被触发。

I don't know if these apply

我不知道这些是否适用

  1. My activity is running inside a tabHost and I was wondering if that can affect service binding in any way.
  2. The Service itself may have a problem, but I can call start service to the same service with the same Intentand it works as expected.
  1. 我的活动在 tabHost 内运行,我想知道这是否会以任何方式影响服务绑定。
  2. 服务本身可能有问题,但我可以使用相同的服务调用启动服务,Intent并且它按预期工作。

Does anyone have experience with this? Please help me out.

有人对此有经验吗?请帮帮我。

Thanks, P.S. I am targeting Android 1.6

谢谢,PS 我的目标是 Android 1.6

回答by codinguser

So I finally figured it out. It turns out that TabSpec cannot bind to activities

所以我终于想通了。原来TabSpec 不能绑定到活动

Using getApplicationContext().bindService instead of just bindService on your activity solves the problem as it is using the higher level application context.

在您的活动上使用 getApplicationContext().bindService 而不是仅使用 bindService 可以解决问题,因为它使用更高级别的应用程序上下文。

Hope this helps someone!

希望这对某人有帮助!

回答by Martijn Courteaux

I now this is old, but I had a slightly different problem. I refactored the package of my project. So the name changed of the files, but Eclipse did not update the manifest file. So, I manually fixed the manifest file and changed the name of the package there as well.

我现在这是旧的,但我有一个稍微不同的问题。我重构了我的项目包。因此文件的名称已更改,但 Eclipse 并未更新清单文件。所以,我手动修复了清单文件,并在那里更改了包的名称。