Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bind Service returns false every time

Tags:

java

android

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

}

like image 789
Arun Abraham Avatar asked Mar 15 '11 05:03

Arun Abraham


1 Answers

I had the same problem. After a time of studying, I found out that our application does not know which service to be bound. This is because either we didn't declare the service in the manifest file, or we declared it in the wrong way.

In my case, I declare it as:

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

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

By this way, Android will understand that the service has the package as vn.abc.SocketService, but in fact, in my code structure, my service has the package com.tung.SocketService (packages here are just examples). That is the reason why Android can not find the service I declared in the manifest file.

like image 185
Xuan Tung Vu Avatar answered Oct 31 '22 22:10

Xuan Tung Vu