如何使AsyncTask的等待,直到GCM广播接收 [英] How to make Asynctask wait untill GCM Broadcast is received

查看:189
本文介绍了如何使AsyncTask的等待,直到GCM广播接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低于code登记我的GCM设备

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);
    }
}

现在,有一件事我知道的是,我会得到通过广播注册ID。但是,我怎么能做出这样的AsyncTask等待,直到接收到广播?

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?

目前,这code 的System.out.println(REGID ==========&gt;中+ REGID);
打印什么,但是当我运行活动第二次,我收到了寄存器ID;

Currently, this code System.out.println("regid ==========>"+regid); prints nothing, but when I run the activity second time, I am receiving the register id;

推荐答案

在我看来,你不必等待。请参阅我下面的示例code。希望这有助于!

In my opinion, you don't have to wait. Refer to my following sample code. Hope this helps!

首先,我检查共享preferences,如果为空,得到谷歌的服务器令牌(设备注册ID)。令牌将被存储在共享preferences用于其他用途/提供至服务器端应用

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();
}   
}

这篇关于如何使AsyncTask的等待,直到GCM广播接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆