2012-06-05 31 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

相關問題