在安卓中与服务绑定的问题

4

我创建了一个名为LocalService的服务,并希望绑定它,以便它可以执行某些操作并将结果返回给调用客户端。我一直在尝试让安卓开发网站上的示例正常工作,但是eclipse在此方法上给出编译时错误:

void doBindService() {
    // Establish a connection with the service. We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

在“Binding.this”下有一条红线,Eclipse给出了以下提示信息:“Binding cannot be resolved to a type”。我该怎么办?

2个回答

5

只需删除绑定,代码应该像这样:

bindService(new Intent(this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
< p > Intent 构造函数的第一个参数是 Context。如果您从 Activity 类中调用它(任何 Activity 实例也是 Context),上面的代码将起作用。如果不是,则始终可以使用应用程序上下文。


1

看起来Binding只是你放置doBindService()的类名(很可能是Activity?)。如果你的类名不同,要么将其重命名为Binding,要么用MyClass.thisthis替换所有的Binding.this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接