onServiceConnected()不叫不叫、onServiceConnected

2023-09-06 03:32:09 作者:我囍举纲!

我有使用后台服务的问题。 我从2活动使用该服务。

I have a problem with using a background service. I am using the service from 2 activities.

第一个活动启动服务 startService(意向)和绑定将其与 bindService(意向,mConnection,Context.BIND_AUTO_CREATE);

The first activity starts the Service with startService(intent) and the binds to it with bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

这完美的作品,我可以在 onServiceConnected发送消息()

This works perfectly and I can send a Message in onServiceConnected().

第二项活动只绑定到服务(因为它已经开始),再次使用 bindService(意向,mConnection,Context.BIND_AUTO_CREATE);

The second activity only binds to the Service (since it's already started), again using bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

现在这里是我的问题: 在第二个活动我有一个按钮应使用该服务时,我preSS它。 但是, onServiceConnected()永远不会被调用这项活动,所以我不能让粘合剂创建一个使者发送消息到服务器。

Now here is my problem: In the second activity I have a Button which should use the service when I press it. However, the onServiceConnected() is never called in this activity, so I can not get the binder to create a Messenger to send a Message to the server.

有谁知道什么时候 onServiceConnected()只调用,或者它可能是在等待什么? 或者,也许别的东西来解决这个问题呢?

Does anyone know when onServiceConnected() is called exactly, or what it is possibly waiting for? Or maybe something else to fix this problem?

编辑: bindService 返回false,这是什么原因

bindService returns false, what could cause this?

在此先感谢

推荐答案

在一些调查研究,我发现这是Android的一个已知的问题。

After some more research, I discovered this is a known issue in Android.

第二个活动,我谈论的是它被用作内的内容活性的 TabActivity

The second activity I was talking about was an activity which is used as content within a TabActivity.

要解决这个问题的方法是调用 bindService(...)应用程序上下文,而不是在活动环境中使用 getApplicationContext( ).bindService(...)

The way to fix this was to call bindService(...) on the application context, instead of on the activity context using getApplicationContext().bindService(...)

 
精彩推荐
图片推荐