2012-12-11 26 views
0

我有一个主要活动(OceanintelligenceActivity)。在这个活动中,我注册了推送通知设备,同时我注册了一个接收器,它显示一个对话框,并根据从我的服务器发送的信息启动适当的活动。这是我使用注册设备和接收器的代码:如何在BroadcastReceiver上的当前可见活动上显示对话框?

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)。这一行将返回与我想要开始的活动相关的意图。

每当有通知onReceive被调用,但对话框只显示如果我在主要活动。所以如果应用程序在不同的活动上,onReceive仍然被调用,但对话框不显示,因此我无法开始正确的活动。

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

回答

0

玩这个和搜索谷歌我遇到了一个解决方案。这不是最好的,但它的工作原理。我仍然无法相信在Android中获取当前上下文并不容易。所以这就是我设法显示对话框,而不管当前活动是什么:在单例类(AppDelegate)上我有一个类型为Context的公共静态属性,并且在每个活动上我重写了onResume方法,并将上下文设置为当前的活动像这样AppDelegate.CURRENT_CONTEXT = this。然后在我的对话框中:AlertDialog.Builder(AppDelegate.CURRENT_CONTEXT).....

相关问题