2011-03-15 59 views
12
boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE); 

綁定服務始終返回我假返回false ......誰能告訴我,我可能做了可能出現的錯誤...Android的服務綁定,每次

服務代碼如下

public class SocketService extends Service{ 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return myBinder; 
} 

private final IBinder myBinder = new LocalBinder(); 

public class LocalBinder extends Binder { 
    public SocketService getService() { 
     return SocketService.this; 
    } 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

public void IsBoundable(){ 
    Toast.makeText(this,"Is bound", Toast.LENGTH_LONG).show(); 
} 

public void onStart(Intent intent, int startId){ 
    super.onStart(intent, startId); 
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

}

服務控制器代碼如下:

public class SocketServiceController extends Activity{ 
private SocketService mBoundService; 
private Boolean mIsBound; 
public SocketServiceController ssc; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ssc = this; 
    setContentView(R.layout.telnet); 
    Button startButton = (Button)findViewById(R.id.button1); 
    Button endButton = (Button)findViewById(R.id.button2); 
    Button bindButton = (Button)findViewById(R.id.button3); 
    startButton.setOnClickListener(startListener); 
    endButton.setOnClickListener(stopListener); 
    //bindButton.setOnClickListener(this); 
    TextView textView = (TextView)findViewById(R.id.textView1); 
} 

    private ServiceConnection mConnection = new ServiceConnection() { 
    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mBoundService = ((SocketService.LocalBinder)service).getService(); 

    } 
    @Override 
    public void onServiceDisconnected(ComponentName className) { 
     mBoundService = null; 
    } 
}; 

private void doBindService() { 
    boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE); 
    mIsBound = true; 
    //mBoundService.IsBoundable(); 
} 


private void doUnbindService() { 
    if (mIsBound) { 
     // Detach our existing connection. 
     unbindService(mConnection); 
     mIsBound = false; 
    } 
} 

private OnClickListener startListener = new OnClickListener() { 
    public void onClick(View v){ 
     startService(new Intent(SocketServiceController.this,SocketService.class)); 
     doBindService(); 
    }    
}; 

private OnClickListener stopListener = new OnClickListener() { 
    public void onClick(View v){ 
     stopService(new Intent(SocketServiceController.this,SocketService.class)); 
    }    
}; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    doUnbindService(); 
} 

}

回答

1

我認爲這個問題可能是在綁定service.I m使用下面的代碼綁定service.Its正確返回true。

boolean flag=bindService(mService, mConnection, MODE_PRIVATE); 

MSERVICE -is服務對象, mConnection-是serviceConnection對象 模式

有可能是代碼中的小變化

boolean isBound = bindService(mBoundService, mConnection, Context.BIND_AUTO_CREATE); 

它可能工作.. 有一個偉大的一天...

+0

嘿...我仍然越來越虛假....我在模擬器中做..我應該在除了 ...之外的清單文件中添加任何額外的東西...? – 2011-03-15 06:40:27

+0

已經花了相當長的時間來解決這個問題......這個代碼中的其他錯誤? – 2011-03-15 06:42:11

+5

最後得到它:getApplicationContext()。bindService不得不使用這個作爲tabspec不能綁定到活動..我在一個標籤內調用。 – 2011-03-15 06:46:18

-2

我有同樣的錯誤,原因是我忘了啓動服務器冰。

+2

綁定應該自動啓動服務。 – Sam 2015-02-13 10:20:15

30

我有同樣的問題。經過一段時間的學習,我發現我們的應用程序不知道要綁定哪個服務。這是因爲我們沒有在清單文件中聲明服務,或者我們以錯誤的方式聲明它。

在我的情況,我把它聲明爲:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="vn.abc" 
................. 

<service android:name=".SocketService" > 
    </service> 

通過這種方式,Android將瞭解該服務具有封裝vn.abc.SocketService,但實際上,在我的代碼結構,我的服務有包com.tung.SocketService(這裏的軟件包只是例子)。這就是爲什麼Android無法找到我在清單文件中聲明的服務的原因。

+2

謝謝!那是我的問題。想知道怎麼沒有人提出這個建議.. – Mugen 2013-03-12 19:07:42

+2

它是一個很好的做法來檢查我們使用的服務是否在我們的清單上。,謝謝軒彤武 – 2013-06-15 07:57:27

+1

也請確保''只是* '但不是''。 – gladed 2015-06-02 05:20:14

1

我有一個類似的錯誤。它原來是由於代碼這兩個塊之間的差異:

@Override 
public IBinder onBind(Intent arg0) 
{ 
    return new MyBinder(); 
} 

和:

private final IBinder mBinder = new MyBinder(); 

@Override 
public IBinder onBind(Intent arg0) 
{ 
    return mBinder; 
} 

第一塊不工作,第二塊一樣。

我希望這可以幫助節省別人花費我追蹤這些的時間。

0

我解決了做一個新項目和複製代碼的問題。問題可能與包名有關。

奇怪的是Android上的工作室,我有以下情況(項目不工作):

  • 包的名稱:com.company.proj。應用

    外面可以看到在新的項目中,我有(工作)同步服務器

這奇怪的阻塞服務器:服務器

  • 應用程序的名稱:SPEEDTEST
  • 名稱服務器:包裝

    • 名稱:應用程序的com.company.proj.server.speedtest
    • 名稱:SPEEDTEST
    • 名稱服務器:同步服務器

    通知應用程序的名稱是如何包的最後一個元素。

    第一種情況允許被正確地執行的應用程序,將消息發送到所述服務而不是從不同的應用程序(該標誌機器人:導出=「真」被正確地在這兩種情況下設定)訪問該服務

  • 0

    我認爲綁定服務標誌設置錯誤。 您應該在您的服務連接中設置標誌。

    private ServiceConnection mServiceConnection = new ServiceConnection() { 
    
        @Override 
        public void onServiceDisconnected(ComponentName name) { 
         mServiceBound = false; 
        } 
    
        @Override 
        public void onServiceConnected(ComponentName name, IBinder service) { 
         BoundService.MyBinder myBinder = (BoundService.MyBinder) service; 
         mBoundService = myBinder.getService(); 
         mServiceBound = true; 
        } 
    

    };

    我做了一個簡單的例子在github。 https://github.com/wingsum93/Bind_Service_Example