2012-06-22 39 views
1

我在面向stub()綁定服務時遇到錯誤。Android綁定服務調用接口問題`

這裏是我的連接代碼:

class LogConnection implements ServiceConnection { 

    public void onServiceConnected(ComponentName className, 
      IBinder boundService) { 
     logService = ILogService.Stub.asInterface((IBinder) boundService); 
    } 

,但它不是爲我工作。

的logcat:

06-22 12:17:28.632: I/dalvikvm(1973): Could not find method com.sam.logservice.ILogService$Stub.asInterface, referenced from method com.sam.logclient.LogClientActivity$LogConnection.onServiceConnected 
06-22 12:17:28.662: W/dalvikvm(1973): VFY: unable to resolve static method 28: Lcom/sam/logservice/ILogService$Stub;.asInterface (Landroid/os/IBinder;)Lcom/sam/logservice/ILogService; 
06-22 12:17:28.662: D/dalvikvm(1973): VFY: replacing opcode 0x71 at 0x0009 
06-22 12:17:28.662: D/dalvikvm(1973): VFY: dead code 0x000c-0016 in Lcom/sam/logclient/LogClientActivity$LogConnection;.onServiceConnected (Landroid/content/ComponentName;Landroid/os/IBinder;)V 
06-22 12:17:28.702: W/ActivityManager(61): Unable to start service Intent { cmp=com.sam.logclient/com.sam.logservice.ILogService }: not found 

編輯: 我下面的例子以下鏈接:

Remote Service call

當我沒有在那個時候我在同一襲擊調試代碼 位置

logService = ILogService.Stub.asInterface((IBinder) boundService); 

希望你有一些解決方案。

請讓我知道你是否想要更多的數據來推斷原因。

回答

0

在你的代碼中,我觀察到那就是你爲什麼要將boundService投給IBinder,實際上沒有必要將它轉換爲只傳遞它。 所以,你的線路應該是這樣

logService = ILogService.Stub.asInterface(boundService); 

確保寫在同一個包服務..

在您的擴展服務只是創建接口的對象

ILogService.Stub logService = new ILogService.Stub(); 

和類onBind方法返回它如下所示

@Override 
    public IBinder onBind(Intent intent) { 
     return logService; 
    } 

Hope這個解釋適用於你。

+0

no..I在另一個包中綁定服務..它不工作 – NovusMobile

+0

發表一些代碼可以在另一個包中綁定服務很好.. – MobileEvangelist