0

我目前在 android 中使用 Google GCM 推送通知服务。我已经完成了 PUSH 并且效果很好。现在我必须向我的服务器发送一些成功注册到 Google 服务器的设备详细信息。所以现在我需要一个 Activity 对象引用。

   protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        displayMessage(context, getString(R.string.gcm_registered));           
        registerToMyServer(context, registrationId); //initiate a rest call
        // the context above returns application context not the activity context
    }

   public void registerToMyServer(Activity activity, String rid){
         //handing REST
   }

任何人都可以在这方面帮助我。我们如何设法从 GCMBaseIntentService 子类的 onRegistered() 方法获取 Activity 引用。

4

1 回答 1

1

为什么需要对 Activity 的引用?如果您不需要用户交互,则不需要 Activity。

现在我必须向我的服务器发送一些成功注册的设备详细信息......

这可以在您当前的 onRegistered 方法中完成(但当然您应该创建一个处理此问题的类)。

您必须了解的另一件事是 GCMBaseIntentService 是一项服务......因此,当您完成注册时,您的活动可能存在也可能不存在。因此,请重新考虑设计。

您不需要 Activity 参考来获取电话详细信息 Activity 从 Context 扩展,因此您可以从提供的 Context 中获取它。

 ↳  android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity

有关更多信息,请查看以下示例:C:\Users\USERNAME\android-sdks\extras\google\gcm\samples\gcm-demo-client\src\com\google\android\gcm\demo\app\ GCMIntentService.java 和 ServerUtilities.java

于 2013-01-29T16:07:05.150 回答