onServiceConnected()未调用

Leo*_*ord 17 android android-service

我在使用后台服务时遇到问题.
我正在使用2项活动的服务.

第一个活动启动Servicewith startService(intent)并与之绑定bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

这完美的作品,我可以送MessageonServiceConnected().

第二个活动仅与Service(因为它已经开始)绑定,再次使用bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

现在这是我的问题:
在第二个活动中Button,当我按下它时,我应该使用该服务.
但是,onServiceConnected()在此活动中从未调用过,因此我无法使用绑定器创建一个Messenger发送Message到服务器的活动.

有谁知道什么时候onServiceConnected()被确切地叫,或者它可能在等什么?
或者也许别的东西来解决这个问题?

编辑: bindService返回false,这可能导致什么?

提前致谢

Leo*_*ord 27

经过一些研究,我发现这是Android中的一个已知问题.

我正在谈论的第二项活动是一项活动,用作一项内容TabActivity.

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

  • 你好,我有同样的问题.你能解释一下这个补救措施吗?如果可能,你可以在这里查看我的代码并告诉我应该改变什么?非常感谢.这个错误让我疯狂了一天.我的代码:http://ideone.com/cV5BJa (2认同)

Erd*_* Ay 20

你需要添加:

<service android:name="package.path.to.your.service.class" android:enabled="true"></service>

在Android清单文件中.

  • 对我来说,它不需要.class (2认同)

feb*_*isi 9

在清单中添加绑定..

 <service
            android:name=".MyAccessibilityService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

</service>
Run Code Online (Sandbox Code Playgroud)

MyAccessibilityService您的服务名称.


hep*_*zoj 7

它刚刚发生在我身上.
不要忘记在onbind返回之前实例化您的binder类.愚蠢的错误,但它发生了.希望这将有助于未来的人.

private YourBinder thisBinder;

oncreate
{
  thisBinder = new YourBinder(); //don't forget this.
}


public class YourBinder extends Binder
{

}

public IBinder onBind(Intent intent) 
{
  return thisBinder;
}
Run Code Online (Sandbox Code Playgroud)