1

我想在 xwalkview 加载时将我的应用程序注册到 GCM,但是我无法在 XWalkResponceClient 中访问 GCM 的上下文和 getInstance()。

@Override
public void onLoadStarted(XWalkView view, String url) {
  super.onLoadStarted(view, url);
  if (TextUtils.isEmpty(regId)) {
        regId = registerGCM();
        Log.d("RegisterActivity", "GCM RegId: " + regId);
    } else {
        Toast.makeText(getApplicationContext(),
                "Already Registered with GCM Server!",
                Toast.LENGTH_LONG).show();
    }
}

public String registerGCM() {

    gcm = GoogleCloudMessaging.getInstance(this);
    regId = getRegistrationId(context);
    System.out.println("Inside registerGCM method"+regId);
    if (TextUtils.isEmpty(regId)) {

        registerInBackground();

        Log.d("RegisterActivity",
                "registerGCM - successfully registered with GCM server - regId: "
                        + regId);
    } else {
        Toast.makeText(getApplicationContext(),
                "RegId already available. RegId: " + regId,
                Toast.LENGTH_LONG).show();
    }
    return regId;
} 
4

1 回答 1

0

只需像这样制作 XWalkResponceClient 的构造函数

Context context
public XWalkResponceClient(Context context)
{

this.context=context;
}

现在在这里传递这个上下文

regId = registerGCM(context);
于 2016-04-28T12:33:50.403 回答