title: How to make app wait for service connection and authentication
body: <p>I need my app to wait on the loading screen so that a connection to the server may be made, and then enter the users saved credentials automatically using SaveSharedPreference and then move to the next page in MainActivity.</p>
  
  <p>The connection is made through a service with a call in the onCreate blocks with:</p>
  
  <p><code>startService(new Intent(LoggingIn.this, MessagingService.class));</code></p>
  
  <p>and the users credentials are taken from SaveSharedPreference  and sent to the server within try/catch blocks as:</p>
  
  <pre><code>                result = imService.authenticateUser(
                          SaveSharedPreference
                                  .getUserName(getApplicationContext()),
                          SaveSharedPreference
                                  .getPassword(getApplicationContext()));
  </code></pre>
  
  <p>At present, the app simply boots up and then crashes. I tried using Thread.sleep(500) but that simply shuts the app down, and posts this error:</p>
  
  <p><img src="https://i.stack.imgur.com/Yu7OF.png" alt="enter image description here"></p>
  
  <ol>
  <li><p>How can I get the functionality as outlined above and make my app wait for a connection to the server prior to sending <code>authenticateUser</code></p></li>
  <li><p>Wait for the successful logging in with <code>authenticateUser</code> before my app tries to launch <code>MainActivity</code>?</p>
  
  <pre><code>    setContentView(R.layout.loading_splash_page);
  
      // If already logged in, pull the information and send to server
      // to
      // auto log in
  
      Thread loginThread = new Thread() {
          private Handler handler = new Handler();
  
          @Override
          public void run() {
              // String result = null;
  
              try {
  
                  result = imService.authenticateUser(
                          SaveSharedPreference
                                  .getUserName(getApplicationContext()),
                          SaveSharedPreference
                                  .getPassword(getApplicationContext()));
  
              } catch (UnsupportedEncodingException e) {
  
                  e.printStackTrace();
              }
  
              if (result == null || result.equals(AUTHENTICATION_FAILED)) {
                  /*
                   * Authenticatin failed, inform the user
                   */
                  handler.post(new Runnable() {
                      public void run() {
                          Toast.makeText(
                                  getApplicationContext(),
                                  R.string.make_sure_username_and_password_correct,
                                  Toast.LENGTH_LONG).show();
  
                          // showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
                      }
                  });
  
              } else {
  
                  /*
                   * if result not equal to authentication failed, result
                   * is equal to friend and group list of the user 0: is
                   * for friends, 1: is for groups
                   */
                  handler.post(new Runnable() {
                      public void run() {
                          Intent i = new Intent(LoggingIn.this,
                                  MainActivity.class);
                          startActivity(i);
                          LoggingIn.this.finish();
  
                      }
                  });
  
              }
  
          }
      };
      loginThread.start();
  </code></pre></li>
  </ol>
  
  <p><strong>ADDITION</strong> The service merhods for imService:</p>
  
  <pre><code>private ServiceConnection mConnection = new ServiceConnection() {
          public void onServiceConnected(ComponentName className, 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 = ((MessagingService.IMBinder) service).getService();
  
              if (imService.isUserAuthenticated() == true) {
                  // Intent i = new Intent(LoggingIn.this, ListOfFriends.class);
                  Intent i = new Intent(LoggingIn.this, MainActivity.class);
                  startActivity(i);
                  LoggingIn.this.finish();
              }
          }
  
          public void onServiceDisconnected(ComponentName className) {
              // This is called when the connection with the service has been
              // unexpectedly disconnected -- that is, its process crashed.
              // Because it is running in our same process, we should never
              // see this happen.
              imService = null;
              Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
                      Toast.LENGTH_SHORT).show();
          }
      };
  </code></pre>