How to make Asynctask wait untill GCM Broadcast is received

余生长醉 提交于 2019-12-08 10:15:58

问题


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;


回答1:


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.

public class MainActivity extends Activity {

Context mContext = this;   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    SharedPreferences appPrefs = mContext.getSharedPreferences("com.example.gcmclient_preferences", Context.MODE_PRIVATE);
    String token = appPrefs.getString("token", "");
    if (token.isEmpty()) {
        try {
            getGCMToken("0123456789"); // Project ID: xxxx Project Number: 0123456789 at https://console.developers.google.com/project/...
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    ...
}
...

private void getGCMToken(final String senderId) throws Exception {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected String doInBackground(Void... params) {
            String token = "";
            try {
                InstanceID instanceID = InstanceID.getInstance(mContext);
                token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                if (token != null && !token.isEmpty()) {
                    SharedPreferences appPrefs = mContext.getSharedPreferences("com.example.gcmclient_preferences", Context.MODE_PRIVATE);
                    SharedPreferences.Editor prefsEditor = appPrefs.edit();
                    prefsEditor.putString("token", token);
                    prefsEditor.commit();
                }
                Log.i("GCM_Token", token);
            } catch (IOException e) {
                e.printStackTrace();
            }    
            return null;            
        }           
    }.execute();
}   
}


来源:https://stackoverflow.com/questions/32110963/how-to-make-asynctask-wait-untill-gcm-broadcast-is-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!