显示 AlertDialog 的异步任务

发布于 2024-11-17 03:33:45 字数 2687 浏览 1 评论 0原文

在过去的几天里,我无法解决对话框的问题。我正在运行一个线程来显示对话框 5000 毫秒并将其删除。我正在尝试举杯(“成功”)。问题是我也第二次收到该对话框。我是 Android 开发新手,我需要使用异步任务来解决这个问题,以便我可以延迟第二个线程并显示一个带有肯定按钮而不是吐司的警报dialog.builder。我花了很多时间,但我很困惑如何实现这个

这里我将我的凭据发送到服务器,在发送时我显示了一个 5000 毫秒的进度对话框,我想要一个单独的线程来显示dialog.builder 的积极信息按钮。(当用户在 logcat 中得到响应时,我应该检查 logcat 中的responsecode==1 或不从 logcat 中显示生成器)

如果可能的话,请有人帮助我用一些代码片段来解决这个问题。

提前致谢。

这是我需要实现异步任务的代码

Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                    try {
                        while (counter < 2) {
                            showmsg(0);
                            Thread.sleep(5000);
                            removeDialog(0);
                            // Toast.makeText(Register.this, "Registerd", Toast.LENGTH_LONG).show();
                            showmsg(1);
                            // toast.show();

                            Thread.sleep(1000);

                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        ++counter;
        dialog = new ProgressDialog(this);
        if (counter == 1) {
            dialog.setMessage("Registering...");
        }
        else {
            String resultsRequestSOAP = "";
            if (Log.v("TAG", String.valueOf(resultsRequestSOAP)) == 1)
                ;
            {
                Context context = getApplicationContext();
                CharSequence text = "Registerd";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}

public void showmsg(int actionsToBePerformedOnScreen) {
    Message msg = new Message();
    msg.what = actionsToBePerformedOnScreen;
    handler.sendMessage(msg);

}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:

            showDialog(0);

            break;

        case 1:
            // clear all images in the list
            removeDialog(0);
            break;

        }

    };
};

For the past few days, I haven't been able to solve an issue with my dialog box. I am running a thread to show the dialog box for 5000ms and removing it. and I am trying to show a toast("SUCCESS"). The problem is I am getting the dialog box for the second time also. I am new to android development and I need to solve this with Async Task so that I can delay the second thread and show a alert dialog.builder with a positive button instead of toast. I goggled a lot but I confused to to implement this

Here I am sending my credentials to server and while sending I am showing a progress dialog box for 5000ms and I want to have a separate thread in order to show the dialog.builder with a positive button.( When the user get a response in the logcat for that I should check the responsecode==1 or not from the logcat to show the builder)

plz someone help me to solve this with some code snippet if possible.

Thanks in advance.

this is the code where I need to implement Async task

Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                    try {
                        while (counter < 2) {
                            showmsg(0);
                            Thread.sleep(5000);
                            removeDialog(0);
                            // Toast.makeText(Register.this, "Registerd", Toast.LENGTH_LONG).show();
                            showmsg(1);
                            // toast.show();

                            Thread.sleep(1000);

                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        ++counter;
        dialog = new ProgressDialog(this);
        if (counter == 1) {
            dialog.setMessage("Registering...");
        }
        else {
            String resultsRequestSOAP = "";
            if (Log.v("TAG", String.valueOf(resultsRequestSOAP)) == 1)
                ;
            {
                Context context = getApplicationContext();
                CharSequence text = "Registerd";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}

public void showmsg(int actionsToBePerformedOnScreen) {
    Message msg = new Message();
    msg.what = actionsToBePerformedOnScreen;
    handler.sendMessage(msg);

}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:

            showDialog(0);

            break;

        case 1:
            // clear all images in the list
            removeDialog(0);
            break;

        }

    };
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-11-24 03:33:45

简单:在onPreExecute中显示对话框,在doInBackground中注册()并在onPostExecute中隐藏对话框。最后,在 onclick 中执行 new RegisterTask().execute()

 private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
  private final ProgressDialog dialog = new ProgressDialog(YourClass.this);

  protected void onPreExecute() {
     this.dialog.setMessage("Signing in...");
     this.dialog.show();
  }

  protected Boolean doInBackground(final Void unused) {
     return Main.this.register(); //don't interact with the ui!
  }

  protected void onPostExecute(final Boolean result) {
     if (this.dialog.isShowing()) {
        this.dialog.dismiss();
     }
     if (result.booleanValue()) {
     //also show register success dialog
     }
  }
}

Easy: show dialog onPreExecute, register() in doInBackground and hide dialog in onPostExecute. Finally, do new RegisterTask().execute() in your onclick.

 private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
  private final ProgressDialog dialog = new ProgressDialog(YourClass.this);

  protected void onPreExecute() {
     this.dialog.setMessage("Signing in...");
     this.dialog.show();
  }

  protected Boolean doInBackground(final Void unused) {
     return Main.this.register(); //don't interact with the ui!
  }

  protected void onPostExecute(final Boolean result) {
     if (this.dialog.isShowing()) {
        this.dialog.dismiss();
     }
     if (result.booleanValue()) {
     //also show register success dialog
     }
  }
}
忆悲凉 2024-11-24 03:33:45

为什么不使用 Android AsyncTask
例如:

public class MyPreloader extends AsyncTask<InputObject, Void, OutputObject>{
private Context context;
private ProgressDialog dialog;

public MyPreloader(Context context){
    this.context = context;
}

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(context);
    dialog.setMessage("Please wait...");
    dialog.setIndeterminate(true);
    dialog.show();
    super.onPreExecute();
}   

@Override
protected ResponseBase doInBackground(InputObject... params) {
InputObject input = params[0]; 
//some code for background work
    }

@Override
protected void onPostExecute(OutputObject result) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    super.onPostExecute(result);
}

Why you don't use Android AsyncTask?
For example:

public class MyPreloader extends AsyncTask<InputObject, Void, OutputObject>{
private Context context;
private ProgressDialog dialog;

public MyPreloader(Context context){
    this.context = context;
}

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(context);
    dialog.setMessage("Please wait...");
    dialog.setIndeterminate(true);
    dialog.show();
    super.onPreExecute();
}   

@Override
protected ResponseBase doInBackground(InputObject... params) {
InputObject input = params[0]; 
//some code for background work
    }

@Override
protected void onPostExecute(OutputObject result) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    super.onPostExecute(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文