0

我有一个程序负责在几秒钟后在前台显示 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 没有显示!请帮助我!谢谢!

4

3 回答 3

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(); 
于 2012-06-05T06:42:08.317 回答
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();       
            }
        }
    }                       
} 
于 2012-06-05T06:51:47.580 回答
0

您不能显示来自服务的对话框。而是使用像 Dialog 这样的 Activity 主题,为 Activity(在清单中)设置主题,如下所示:

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

此外,你不应该使用TimerTaskHandler而是使用,像这样:http: //developer.android.com/resources/articles/timed-ui-updates.html

于 2012-06-05T07:39:08.757 回答