检查特定的alertdialogbox是否存在android

时间:2013-08-06 06:18:24

标签: android udp

我有一个UDP服务器,可以将数据发送到客户端。数据的显示(在客户端)使用对话框。问题是如果我有多个相同数据的发送,将有多个具有相同值的对话框。我想删除其他对话框以保留具有唯一值的对话框。

2 个答案:

答案 0 :(得分:1)

 if(dialog != null && dialog.isShowing())
 {  
   return;
 }

当任务完成时使用 dialog.dismiss();

答案 1 :(得分:0)

如何根据您收到的数据的唯一标识符使用某种Set?标识符应与相同数据的多个“发送”相同。

private AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(Context.this);
private Set<Integer> mShownDialogs = new HashSet<Integer>();

public void onReceive(final MyData data) {
    final Integer dataHash = data.getUniqueHash();
    if (!mShownDialogs.contains(dataHash)) {
        mShownDialogs.add(dataHash);

        mDialogBuilder.setTitle(data.getTitle());
        mDialogBuilder.setMessage(data.getMessage());
        AlertDialog dialog = mDialogBuilder.create();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // If I want to show a dialog with the same dataHash some
                // time in the future, I should remove from set.
                mShownDialogs.remove(dataHash);
            }
        });
        dialog.show();
    } else {
        // Discard the data?
    }
}
相关问题