在我的第一个活动中服务已连接,但是当我在其他活动中将服务绑定时,它不会绑定,而是提供“空”值。
这是我启动服务的第一个活动
如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.O){
Intent intent = new Intent(SplashActivity.this, TCPConnectionService.class);
startForegroundService(intent);
} else {
Intent intent = new Intent(SplashActivity.this, TCPConnectionService.class);
startService(intent);
}
这是我绑定服务的第二个活动,
TCPConnectionService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
//EDITED PART
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBoundService = ((TCPConnectionService.LocalBinder) service).getService();
mIsBound = true;
Log.e("SERVICE CONNECTED>>>>", "yes");
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBoundService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Log.e("SERVICE CONNECTED 222>>", "yes");
doBindService();
sendAuthenticate();
}
private void doBindService() {
getApplicationContext().bindService(new Intent(getApplicationContext(), TCPConnectionService.class), mConnection, Context.BIND_AUTO_CREATE);
if (mBoundService != null) {
mBoundService.IsBoundable();
}
}
边界不会发生,任何帮助都很好