启动后绑定服务

时间:2019-05-29 11:22:32

标签: android service

在我的第一个活动中服务已连接,但是当我在其他活动中将服务绑定时,它不会绑定,而是提供“空”值。

这是我启动服务的第一个活动

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

边界不会发生,任何帮助都很好

0 个答案:

没有答案