예제 #1
0
  private synchronized void start() {
    log("Starting service...");

    // Do nothing, if the service is already running.
    if (mStarted == true) {
      Log.w(TAG, "Attempt to start connection that is already active");
      return;
    }

    // Establish an MQTT connection
    connect();

    // Register a connectivity listener
    registerReceiver(
        mConnectivityChanged, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
  }
예제 #2
0
  private synchronized void stop() {
    // Do nothing, if the service is not running.
    if (mStarted == false) {
      Log.w(TAG, "Attempt to stop connection not active.");
      return;
    }

    // Save stopped state in the preferences
    setStarted(false);

    // Remove the connectivity receiver
    unregisterReceiver(mConnectivityChanged);
    // Any existing reconnect timers should be removed, since we explicitly
    // stopping the service.
    cancelReconnect();

    // Destroy the MQTT connection if there is one
    if (mConnection != null) {
      mConnection.disconnect();
      mConnection = null;
    }
  }