title: How to implement Back Off in Google Cloud Messaging Api when Service Not Available
body: <p>I have used Google Cloud Messaging Api to implement GCM functionality but I need to fix Service Not Available error. For that I am using back off but I dont know after making the thread sleep.. How can I set register the device again.</p>
  
  <pre><code> private void registerInBackground() {
          new AsyncTask&lt;Void, Void, String&gt;() {
              @Override
              protected String doInBackground(Void... params) {
                  String msg = "";
                  try {
                      if (gcm == null) {
                          gcm = GoogleCloudMessaging.getInstance(SettingsActivity.this);
                      }
                      regid="";
                      regid = gcm.register(SENDER_ID);
                      msg = "Device registered, registration ID=" + regid;
  
                      // You should send the registration ID to your server over HTTP, so it
                      // can use GCM/HTTP or CCS to send messages to your app
  
                      sendRegistrationIdToBackend();
  
                      // For this demo: we don't need to send it because the device will send
                      // upstream messages to a server that echo back the message using the
                      // 'from' address in the message.
  
                      // Persist the regID - no need to register again.
                      storeRegistrationId(SettingsActivity.this, regid);
                  } catch (IOException ex) {
                      Log.e("Error on register",""+ex);
                      msg = "Error :" + ex.getMessage();
                      ex.printStackTrace();
                      // If there is an error, don't just keep trying to register.
                      // Require the user to click a button again, or perform
                      // exponential back-off.
  
  
                      if(ex.getMessage().equals(ERROR_SERVICE_NOT_AVAILABLE))
                      {
                          try
                          {
                              for(int n=0;n&lt;5;n++)
                              {
                                  Log.e("service_error","retrying:"+(n+1)+"time");
                                  Thread.sleep((1 &lt;&lt; n) * 1000 + randomGenerator.nextInt(1001));
                                 /* gcm=GoogleCloudMessaging.getInstance(SettingsActivity.this);
                                  regid=gcm.register(SENDER_ID);
                                  Log.e("back off reg id:",""+regid);*/
  
                              }
                          }
                          catch (Exception e)
                          {
                              e.printStackTrace();
                              Log.e("back off error",""+e);
                          }
                      }
  
  
                  }
                  catch (Exception e)
                  {
                      Log.e("Erron on register main exp",""+e);
                      e.printStackTrace();
                  }
                  Log.i("reg resp",msg);
                  return msg;
              }
  </code></pre>