I am using below code to register my device for GCM
private class GetGcmRegId extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pdia = new ProgressDialog(LoginActivity.this);
pdia.setMessage("Please Wait...");
pdia.setCancelable(false);
pdia.setCanceledOnTouchOutside(false);
pdia.show();
}
@Override
protected String doInBackground(Void... params) {
GCMRegistrar.register(LoginActivity.this, Utility.SENDER_ID);
regid = GCMRegistrar.getRegistrationId(LoginActivity.this);
return regid;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pdia.dismiss();
regid = result;
System.out.println("regid ==========>"+regid);
}
}
Now, one thing I know is that I will get register id via a Broadcast. But how can I make this asynctask wait until the broadcast is received?
Currently, this code System.out.println("regid ==========>"+regid);
prints nothing, but when I run the activity second time, I am receiving the register id;
In my opinion, you don't have to wait. Refer to my following sample code. Hope this helps!
Firstly, I check SharedPreferences, if empty, get token (device registration id) from Google server. Token will be stored in SharedPreferences for other use / providing to server-side app.