安卓绑定服务每次都返回 false

2022-09-03 16:41:01
boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);

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

服务代码如下

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

}

服务控制器代码如下:

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

}


答案 1

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

就我而言,我声明为:

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

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

通过这种方式,Android 会理解服务将包作为 ,但实际上,在我的代码结构中,我的服务具有包(这里的包只是示例)。这就是为什么Android找不到我在清单文件中声明的服务的原因。vn.abc.SocketServicecom.tung.SocketService


答案 2

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

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

推荐