2012-09-11 56 views
4

我創建了一個名爲LocalService的服務,我想綁定到它,以便它可以執行某些操作並將結果返回給調用客戶端。我一直試圖讓example of the android dev site的工作,但月食給出了這種方法的編譯時錯誤:在Android中綁定服務的問題

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」之下,日食給我:綁定不能被解析爲一種。我能做什麼?

回答

5

只是刪除綁定,代碼應該是這樣的:

bindService(new Intent(this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE); 

第一個參數的構造函數IntentContext。如果你從你的Activity類調用它,上面的代碼將會工作(Activity的任何實例也是一個上下文)。如果不是,您可以隨時使用應用程序上下文

1

它看起來像Binding只是你把doBindService()(最可能的活動?)的類名稱。如果您的班級被命名,請將其重命名爲Binding或將Binding.this全部替換爲MyClass.thisthis