我有一個程序時,它的前景幾秒鐘後,負責顯示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沒有顯示!請幫助我!謝謝!
你確定你的id-else條件正確導致AlertDialog嗎?你能記錄任何消息來驗證條件工作正常嗎? – waqaslam
你在哪裏運行這段代碼?在活動或服務? – alexanderblom
@alexanderblom 在服務:我哈瓦的TimerTask timer.scheduleAtFixedRate(新的TimerTask(){ showAlertDialog();} – user1184715