Android Bind Service每次都返回false

Aru*_*ham 13 java android

boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

绑定服务总是为我返回假...有人能告诉我可能出现的错误......

服务代码如下

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();
}
Run Code Online (Sandbox Code Playgroud)

}

服务控制器代码如下:

 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();
}
Run Code Online (Sandbox Code Playgroud)

}

Xua*_* Vu 34

我有同样的问题.经过一段时间的学习,我发现我们的应用程序不知道要绑定哪个服务.这是因为要么我们没有在清单文件中声明服务,要么我们以错误的方式声明它.

就我而言,我将其声明为:

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

<service android:name=".SocketService" >
    </service>
Run Code Online (Sandbox Code Playgroud)

通过这种方式,Android将了解该服务具有包vn.abc.SocketService,但事实上,在我的代码结构中,我的服务有包com.tung.SocketService(这里的包只是示例).这就是Android无法找到我在清单文件中声明的服务的原因.

  • 谢谢!!那是我的问题.想知道为什么没有人建议这个.. (2认同)
  • 检查我们使用的服务是否在我们的清单上是一个很好的做法.感谢Xuan Tung Vu (2认同)

Des*_*ves 8

bindService()返回false 的一种非常常见的情况是服务未在 Manifest 中声明。在这种情况下,您应该在清单文件中声明您的服务。

<manifest ... >
   ...
   <application ... >
      <service android:name=".MyService" />
      ...
   </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)


sta*_*ion 1

我认为问题可能出在绑定服务时。我使用以下代码来绑定服务。它正确返回 true。

boolean flag=bindService(mService, mConnection, MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)

mService - 是服务对象,mConnection - 是服务连接对象模式

您的代码可能有一个小变化

boolean isBound = bindService(mBoundService, mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

它可能会起作用......祝你有美好的一天......

  • 终于明白了: getApplicationContext().bindService 必须使用它,因为 tabspec 无法绑定到活动..我在选项卡内调用.. (7认同)