I am trying to get device registration id using GCM. My code for this is contained in AsyncTask, which is called from my main thread.
Main code
try { String deviceId = new Gcm().execute(this.activity).get(5, TimeUnit.SECONDS); Log.i("Login", "User device id returned as " + deviceId); return deviceId; } catch (Exception e) { Log.e("Login", "Exception", e); }
GCM Class
public class Gcm extends AsyncTask<Activity,Void,String> { @Override protected String doInBackground(Activity... params) { Log.i("GCM", "Attempting to get device id"); Activity activity = params[0]; try { Log.i("GCM", "Getting GCM instance"); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(activity.getApplicationContext()); Log.i("GCM", "Registering with GCM"); String regId = gcm.register(PROJECT_NUMBER); Log.i("GCM", "Device registered, registration ID=" + regId); return regId; } catch (IOException e) { throw new IllegalStateException(e); } } }
And here is my log dump
07-28 13:07:39.093 I/GCM﹕ Attempting to get device id 07-28 13:07:39.093 I/GCM﹕ Getting GCM instance 07-28 13:07:39.093 I/GCM﹕ Registering with GCM 07-28 13:07:44.103 E/Login﹕ Exception java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(FutureTask.java:176) at android.os.AsyncTask.get(AsyncTask.java:507) I/GCM﹕ Device registered, registration ID=XXXXXX
Therefore, for some reason, the call to gcm.register () is blocked until my timeout exception is removed. Does anyone know why this might happen?
source share