2012-06-05 33 views
0

我有一个程序时,它的前景几秒钟后,负责显示AlertDialog:如何显示AlertDialog当应用程序在前台Android?

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没有显示!请帮助我!谢谢!

+0

你确定你的id-else条件正确导致AlertDialog吗?你能记录任何消息来验证条件工作正常吗? – waqaslam

+0

你在哪里运行这段代码?在活动或服务? – alexanderblom

+0

@alexanderblom 在服务:我哈瓦的TimerTask timer.scheduleAtFixedRate(新的TimerTask(){ showAlertDialog();} – user1184715

回答

0

您无法显示服务中的对话框。而是使用一个活动主题就像一个对话框,设置为活动主题(在清单)是这样的:

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

而且,你不应该使用TimerTask,使用Handler代替,就像这样:http://developer.android.com/resources/articles/timed-ui-updates.html

+0

你的意思是用对话框主题创建新的活动!并在前台应用程序时调用它? – user1184715

+0

是的,这会按照你的意愿行事。您需要这样做,因为您无法从后台启动对话框。 – alexanderblom

+0

感谢您的帮助!我不能投票给你,因为我没有足够的声望! :)和我们同龄! :) – user1184715

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(); 
+0

如果你只是想显示AlertDialog而不设置它对** AlertDialog **对象的引用,那么你并不需要在show()之前调用'create()'。你可以直接调用'show()'弹出对话框 – waqaslam

+0

它不适合我!但是非常感谢你 – user1184715

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();  
      } 
     } 
    }      
} 
+0

谢谢!我尝试这个,但它没有奏效! :) – user1184715

+0

你有任何错误或仍然不显示AlertDialog? –

+0

没错!只是还没有显示AlertDialog!如果在后台应用程序没问题,但前景不是 – user1184715

相关问题