Java Android绑定服务每次都返回false

Java Android绑定服务每次都返回false,java,android,Java,Android,绑定服务总是为我返回false。。。谁能告诉我我可能会犯的错误 服务代码如下 boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE); } 服务控制器代码如下: public class SocketService extends Service{ @Override public IB

绑定服务总是为我返回false。。。谁能告诉我我可能会犯的错误

服务代码如下

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

服务控制器代码如下:

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

}

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

 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();
}
mService-是服务对象, mConnection-是serviceConnection对象 模式

您的代码中可能有一些小的更改

boolean flag=bindService(mService, mConnection, MODE_PRIVATE);
这可能有用。。
祝您度过愉快的一天。

我也有同样的错误,原因是我忘记启动服务。

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

就我而言,我声明:

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

我也有类似的错误。这是由于这两段代码之间的差异:

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

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

第一个块不起作用,第二个块起作用


我希望这能帮助其他人节省我花在追踪这件事上的时间。

我在做一个新项目并复制其中的代码时解决了这个问题。问题可能与包名有关

奇怪的是,在Android Studio上,我遇到了以下情况(项目不起作用):

  • 包的名称:com.company.proj.server
  • 应用程序名称:speedtest
  • 服务器名称:syncserver
这奇怪地阻止了服务器在应用程序之外被看到

在新项目中,我有(工作):

  • 软件包名称:com.company.proj.server.speedtest
  • 应用程序名称:speedtest
  • 服务器名称:syncserver
请注意应用程序的名称是如何作为包的最后一个元素的


第一种情况允许正确执行应用程序,向服务发送消息,但不允许从其他应用程序访问服务(两种情况下都正确设置了标志android:exported=“true”)

我认为绑定服务标志设置错误。 您应该在服务连接中设置标志

private final IBinder mBinder = new MyBinder();

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

我在github中做了一个简单的示例。

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

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

...
...

嘿。。。我还是有点假。。。。我正在做模拟机。。我是否应该在清单文件中添加除…以外的其他内容?我花了很长时间来解决这个问题。。。这段代码中还有什么错误吗?终于明白了:getApplicationContext().bindService必须使用它,因为tabspec无法绑定到活动..我是在一个选项卡中调用的..@ArunAbraham不知道如何感谢您。三天了,我被困在这里。现在工作了。@AbdulMohsin我很高兴它起到了作用。检查我们使用的服务是否在我们的清单上是一个很好的做法。谢谢。另外,请确保
正好在
内,而不是
内。我不知道需要在清单中注册服务,谢谢!这也解决了我的问题。先生,你救了我的命。请用我喜欢的。如果它可以抛出一些警告或错误,这不会有什么坏处。绑定应该自动启动服务。
 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;
    }
<manifest ... >
   ...
   <application ... >
      <service android:name=".MyService" />
      ...
   </application>
</manifest>