@Override
        public void onServiceConnected(ComponentName name, IBinder service) {
          // This is called when the connection with the service has been
          // established, giving us the service object we can use to
          // interact with the service.  Because we have bound to a explicit
          // service that we know is running in our own process, we can
          // cast its IBinder to a concrete class and directly access it.

          iMService = ((IMService.IMBinder) service).getService();

          if (MainActivity.DEBUG) Log.d("RegisterActivity", "chiamato onServiceConnected");

          if (iMService.isUserLoggedIn()) {
            startActivity(new Intent(RegisterActivity.this, LoggedUser.class));
            RegisterActivity.this.finish();
          }
        }
  private void attemptRegister() {
    if (attempting) return;

    // Reset errors.
    et_username.setError(null);
    et_password.setError(null);
    et_password2.setError(null);

    // Store values at the time of the login attempt.
    username = et_username.getText().toString();
    password = et_password.getText().toString();
    password2 = et_password2.getText().toString();

    boolean cancel = false;
    View focusView = null;

    // Check for username
    if (!cancel && TextUtils.isEmpty(username)) {
      et_username.setError(getString(R.string.it_error_field_required));
      focusView = et_username;
      cancel = true;
    }

    // Check for password.
    if (!cancel && TextUtils.isEmpty(password)) {
      et_password.setError(getString(R.string.it_error_field_required));
      focusView = et_password;
      cancel = true;
    }

    if (!cancel && TextUtils.isEmpty(password2)) {
      et_password2.setError(getString(R.string.it_error_field_required));
      focusView = et_password2;
      cancel = true;
    }

    if (!cancel && !password.equals(password2)) {
      et_password2.setError(getString(R.string.it_error_passwords_are_not_equal));
      focusView = et_password2;
      cancel = true;
    }

    if (cancel) {
      // There was an error; don't attempt login and focus the first
      // form field with an error.
      focusView.requestFocus();
    } else {
      // Show a progress spinner, and kick off a background task to
      // perform the user login attempt.
      tv_register_status_message.setText(R.string.it_register_progress_message);
      showProgress(true);

      if (iMService == null) {
        Tools.showMyDialog(getString(R.string.it_error_no_service), this);
        showProgress(false);
      } else if (!iMService.isNetworkConnected()) {
        Tools.showMyDialog(getString(R.string.it_error_no_network), this);
        showProgress(false);
      } else {

        Thread registerThread =
            new Thread() {
              private Handler handler = new Handler();
              private String errorMsg = "";

              @Override
              public void run() {
                try {
                  iMService.registerUser(username, password);
                } catch (UnknownHostException e) {
                  errorMsg = getString(R.string.it_error_server_not_reachable);
                  if (MainActivity.DEBUG) errorMsg += ": " + e.getMessage();
                } catch (UsernameAlreadyExistsException e) {
                  errorMsg = getString(R.string.it_error_username_already_exists);
                  if (MainActivity.DEBUG) errorMsg += ": " + e.getMessage();
                } catch (CommunicationException e) {
                  errorMsg = getString(R.string.it_error_communication_error);
                  if (MainActivity.DEBUG) errorMsg += ": " + e.getMessage();
                } catch (UserIsAlreadyLoggedInException e) {
                  errorMsg = getString(R.string.it_error_user_already_logged_in);
                  if (MainActivity.DEBUG) errorMsg += ": " + e.getMessage();
                }

                if (errorMsg.equals("")) {
                  handler.post(
                      new Runnable() {

                        @Override
                        public void run() {
                          new AlertDialog.Builder(RegisterActivity.this)
                              .setMessage(getString(R.string.it_successfull_registration))
                              .setCancelable(false)
                              .setNeutralButton(
                                  "Ok",
                                  new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                      //											startActivity(new Intent(RegisterActivity.this,
                                      // MainActivity.class));
                                      RegisterActivity.this.finish();
                                      dialog.cancel();
                                    }
                                  })
                              .create()
                              .show();
                        }
                      });

                  return;
                }

                handler.post(
                    new Runnable() {

                      @Override
                      public void run() {
                        Tools.showMyDialog(errorMsg, RegisterActivity.this);
                        showProgress(false);
                      }
                    });
              }
            };

        registerThread.start();
      }
    }
  }