开发者

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,开发者_如何学Python 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();
}

}


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.


One very common case in which bindService() returns false is if the service was not declared in the Manifest. In that case you should declare your service in manifest file.

<manifest ... >
   ...
   <application ... >
      <service android:name=".MyService" />
      ...
   </application>
</manifest>


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...


I had a similar error. It turned out to be due to the difference between these two blocks of code:

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

and:

private final IBinder mBinder = new MyBinder();

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

The first block doesn't work, the second block does.

I hope this helps save someone else the hours it took me to track this down.


I solved the problem doing a new project and copying the code in it. Probably the problem was related to package name.

Strangely on Android Studio I had the following situation (project not working):

  • name of the package: com.company.proj.server
  • name of the app: speedtest
  • name of the server: syncserver

This strangely blocked the server to be seen outside the app

In the new project I had (working):

  • name of the package: com.company.proj.server.speedtest
  • name of the app: speedtest
  • name of the server: syncserver

notice how the name of the app is the last element of the package.

The first situation allowed the app to be executed correctly, to send messages to the service but not to access the service from a different app (the flag android:exported="true" was correctly set in both cases)


i think the bound service flag is setting wrong. you should set the flag in you service connection .

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

};

i had done a simple exampl in github . https://github.com/wingsum93/Bind_Service_Example


I had the same error and the reason was that I forgot to start the service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜