在GCM的onMessage()中显示对话框

3
我正在将 GCM(Google Cloud Messaging)集成到我的应用程序中。
我按照 Google 的教程进行了设置,并且目前它可以运行。
当 `GCMIntentService` 被调用时,我会在通知栏中显示通知。
现在我有一个方法告诉我应用程序是在前台还是后台。当应用程序在后台时,它可以在通知栏中显示通知,没有问题。
但是,我要如何向用户显示对话框?
当我调用:
AlertDialog.Builder builder = new AlertDialog.Builder(context);

其中,上下文是从onMessage()中传递的给定上下文,我当然会遇到以下错误:

_Notification.showPopUp() Error: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

于是,我尝试用MainActivity.this替换上下文,为此我将其保存在静态变量中;但当我现在运行它时,什么都不会发生,没有错误,也没有对话框出现。

我的对话框代码:

private static AlertDialog.Builder myAlertDialog;

private static void showPopUp(Context context,String kind, String resource_name, Integer resource_id)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to exit?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
        {
        public void onClick(DialogInterface dialog, int id) 
            {
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();
            }
        });

    AlertDialog alert = builder.create();
alert.show();

Log.e("TEST","alert.show()");
}

最后一条日志:alert.show()在logcat中被显示,但没有错误。
规格: 运行在设备上(Galaxy S2) Android 4.0.3
请问有人能告诉我我的代码哪里出了问题,或者是否有任何解决方法?
编辑:
存储MainActivity.this的部分:
private static Context context_forshowingPopUp = null;

onCreate

//Set the context for showing a popup View
_Notification.setContext_forshowingPopUp(this);

AlertDialog.Builder builder = new AlertDialog.Builder(getContext_forshowingPopUp());

public static Context getContext_forshowingPopUp() 
{
    return context_forshowingPopUp;
}

public static void setContext_forshowingPopUp(Context context_forshowingPopUp) 
{
    _Notification.context_forshowingPopUp = context_forshowingPopUp;
}

记录一下:如果应用程序不在前台,您仍然可以使用通知区域。请参阅NotificationManager类。 - Seva Alekseyev
是的,我知道,但当用户已经在应用程序中时,在通知栏中收到通知似乎不太合逻辑。 Android设计模式中有关于此使用情况的任何信息吗? - user1683272
只需在某个静态可访问的位置维护指向当前活动的指针。在应用程序中的每个活动的onResume()中设置它,在onPause()中清除它。如果所有活动都派生自一个自定义的共同基类,则有所帮助。 - Seva Alekseyev
我尝试了,但它不起作用!:/ 有其他的想法吗? - user1683272
如果我将我的活动保存在静态变量中,并且我想显示对话框,什么也不会发生,没有错误,没有logcat条目或其他任何东西。 - user1683272
你尝试过调试那个地方吗?你是否使用了活动上下文来调用对话框? - Seva Alekseyev
1个回答

0

在构建AlertDialog时,您必须使用YourCurrentActivity.this作为上下文。您可以像下面这样解决:

第一类:

  public class Config{
     public static Context context;

    }

当你的活动创建时,只需设置Config.context。
   public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      Config.context=this;
    ...}
    //other stuffs

      }

在OnMessage中

   showPopUp(Config.context,kind, resource_name, resource_id);

我有一个私有静态上下文context_forshowingPopUp;,我做的事情和你完全一样。但是它不起作用。我会更新我的问题,展示这部分。谢谢。 - user1683272
java.lang.RuntimeException: Handler (android.view.ViewRootImpl) {41929ee8} 发送消息到一个已经死亡的线程上的处理程序 - 这是我在日志中看到的解决方案。 - David
我认为这是全局状态错误的另一个原因:保留对主活动的引用并不能保证该活动在需要时仍然存在。 - ereOn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接