如何在BroadcastReceiver上显示当前可见活动的对话框?

时间:2012-12-11 01:02:32

标签: dialog broadcastreceiver

我有一个主要活动(OceanintelligenceActivity)。在此活动中,我注册了设备以进行推送通知,并且我还注册了一个显示Dialog的接收器,并根据从我的服务器发送的信息启动正确的Activity。这是我用来注册设备和接收器的代码:

protected void gcmRegistration(){

    PMApplication thisApp = PMApplication.getInstance();
    AppDelegate delegate = thisApp.getAppDelegate();
    final Context context = this;
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);     
    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.     
    GCMRegistrar.checkManifest(this);

    // Let's declare our receiver
    registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {       
        Log.d("", "Lets register for Push");
        GCMRegistrar.register(this, SENDER_ID);       
    }else {

      if(GCMRegistrar.isRegisteredOnServer(this)) {
          // Skips registration.                          
          String apnsToken = delegate.sso.getAPNSToken();           
          if(!apnsToken.equals(regId)){

            Log.d("", "The Device RegId has changed on GCM Servers");
            // We should let our servers know about this
            ServerUtilities.update(regId, context);
          }


      } else {        

          Log.d("","Is not register on PM Server");               
          // Try to register again, but not in the UI thread.
          // It's also necessary to cancel the thread onDestroy(),
          // hence the use of AsyncTask instead of a raw thread.              
          mRegisterTask = new AsyncTask<Void, Void, Void>() {

              @Override
              protected Void doInBackground(Void... params) {

                  boolean registered = ServerUtilities.register(context, regId);
                  // At this point all attempts to register with the app
                  // server failed, so we need to unregister the device
                  // from GCM - the app will try to register again when
                  // it is restarted. Note that GCM will send an
                  // unregistered callback upon completion, but
                  // GCMIntentService.onUnregistered() will ignore it.
                  if (!registered) {
                      GCMRegistrar.unregister(context);
                  }
                  return null;
              }

              @Override
              protected void onPostExecute(Void result) {
                  mRegisterTask = null;
              }

          };
          mRegisterTask.execute(null, null, null);

      }
    }                         
}

这就是我设置接收器的方式:

private final BroadcastReceiver mHandleMessageReceiver =
        new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        Log.d("","BroadcastReceiver onReceive");  
        notificationIntent = GCMIntentService.getNotificationIntent(context);

        new AlertDialog.Builder(context)
        .setMessage(newMessage+". Would you like to see it right now?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {                                                       
                // Show update                                                              
                startActivity(notificationIntent);                                                              
            }
        })
        .setNegativeButton("No", null).show();                                    
    }
};

GCMIntentService.getNotificationIntent(context)。此行返回我想要启动的活动的Intent。

每当有一个通知onReceive被调用但是Dialog只显示我是否在主活动上。因此,如果应用程序处于不同的活动,onReceive仍会被调用但对话框未显示,因此我无法启动正确的活动。

如何在BroadcastReceiver上显示当前可见活动的对话框?

1 个答案:

答案 0 :(得分:0)

玩这个并在谷歌上搜索我遇到了一个解决方案。它不是最好的,但它有效。我仍然无法相信没有一种简单的方法来获取Android中的当前上下文。因此,无论当前活动是什么,这都是我设法显示Dialog所做的:我在我的单例类(AppDelegate)上有一个Context类型的公共静态属性,并且在每个活动上我重写onResume方法并将Context设置为像这样的当前活动AppDelegate.CURRENT_CONTEXT =这个。然后在我的对话框上:AlertDialog.Builder(AppDelegate.CURRENT_CONTEXT).....