Arun Abraham Arun Abraham - 7 months ago 173
Java Question

Android Bind Service returns false every time

boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);


Bind service always returns false for me... Could anyone tell me the possible errors that i could have made...

Service code is as follows

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();
}


}

Service Controller code is as follows:

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();
}


}

Answer

I think the problem might be while binding the service.I m using the following code to bind the service.Its returning true properly.

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

mService -is the service object, mConnection- is serviceConnection object Mode

There might be a small change in your code

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

It might work.. Have a great day...

Comments