如何在Foreground Android中应用程序时显示AlertDialog?

时间:2012-06-05 06:34:40

标签: android alertdialog

我有一个程序负责显示AlertDialog,它在几秒钟后显示在前台:

    ActivityManager am = (ActivityManager) getSystemServ();
    if (am != null) {
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
        if (taskInfo != null && !taskInfo.isEmpty()) {
            if (taskInfo.get(0) != null && taskInfo.get(0).topActivity != null) {
                if (!MY_CLASS_NAME.equalsIgnoreCase(taskInfo.get(0).topActivity.getClassName())) {

                    new AlertDialog.Builder(this).setMessage("AAAAAAAAAAAAAAAAAAAAAAAAAAAA?")
                    .setTitle("Message").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {                                

                        }  
                    })  
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which){

                        }
                    }).show();                      
                }
            }
        }                       
    }       

但当应用程序转到前台时,AlertDialog没有显示!请帮助我!谢谢!

3 个答案:

答案 0 :(得分:0)

使用create()方法创建如下对话框:

new AlertDialog.Builder(this).setMessage("AAAAAAAAAAAAAAAAAAAAAAAAAAAA?")
                        .setTitle("Message").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {                                

                        }  
                    })  
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which){

                        }
                    }).create().show(); 

答案 1 :(得分:0)

我刚修改了你的代码。请检查并更新您的代码段:

 ActivityManager am = (ActivityManager) getSystemServ();
if (am != null) {
    List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
    if (taskInfo != null && !taskInfo.isEmpty()) {
        if (taskInfo.get(0) != null && taskInfo.get(0).topActivity != null) {
            if (!MY_CLASS_NAME.equalsIgnoreCase(taskInfo.get(0).topActivity.getClassName())) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("AAAAAAAAAAAAAAAAAAAAAAAAAAAA?")
                .setTitle("Message").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {                                

                    }  
                })  
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which){

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

答案 2 :(得分:0)

您无法显示服务中的对话框。而是使用以对话框为主题的Activity,为Activity设置主题(在清单中),如下所示:

android:theme="@android:style/Theme.Dialog"

此外,您不应使用TimerTask,而是使用Handler,如下所示:http://developer.android.com/resources/articles/timed-ui-updates.html