Android ProgressDialog 没有从线程中解散
Android ProgressDialog not dismissing from Thread
我正在开发一个 android 应用程序。
用户注册过程调用发送电子邮件的服务,因此需要几秒钟,例如 5 或 6 秒,这就是我在线程中执行该任务的原因。问题是,对话框永远不会消失。它保持滚动,用户无能为力。这是我的代码:
try
{
final ProgressDialog progDailog = new ProgressDialog(ActividadAltaUsuario.this);
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
URL url = new URL("slowWS");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = IOUtils.toString(in, "UTF-8");
final JSONObject jsonPrincipal = new JSONObject(response);
Boolean success = jsonPrincipal.get("status").toString() == "true";
if (success)
{
ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progDailog.show(ActividadAltaUsuario.this, "Sendind email");
}
});
final String idUsuario = jsonPrincipal.get("idUsuario").toString();
URL url2 = new URL("anotherSlowWS");
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
conn2.setRequestMethod("POST");
InputStream in2 = new BufferedInputStream(conn2.getInputStream());
String response2 = IOUtils.toString(in2, "UTF-8");
JSONObject jsonRtaMail = new JSONObject(response2);
//finish();
}
else
{
//finish();
showToast(jsonPrincipal.get("message").toString());
}
ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progDailog.dismiss();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection" + e.toString());
}
有人可以帮助我吗?
谢谢!
AsyncTask 将是一种比线程更好的方法,将线程中的网络调用替换为使用 AsyncTask。你可以使用这样的东西
private class LongOperation extends AsyncTask<Void, Void, Void> {
@Override
protected String doInBackground(Void... params) {
//Main stuff that needs to be done in background
}
@Override
protected void onPostExecute(Void result) {
//Post Execution this method will be called, handle result accordingly
//You can dismiss your dialog here
}
@Override
protected void onPreExecute() {
//Do initialization relative stuff here
// Initialize your dialog here.
}
}
由于 onPostExecute()
和 onPreExecute()
都在主线程上工作,您可以在此方法中显示和关闭对话框。
UI 控件只能从 UI 线程访问。
通常我在扩展 AsyncTask
的 class 中执行此操作
类似于:
public class MyTask 扩展了 AsyncTask {
protected void onPreExecute() {
//create and display your alert here
progDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Logging ...", true);
}
protected Void doInBackground(Void... unused) {
// here is the thread's work ( what is on your method run()
...
// if we want to show some progress in UI, then call
publishProgress(item)
}
protected void onProgressUpdate(Item... item) {
// theoretically you can show the progress here
}
protected void onPostExecute(Void unused) {
//dismiss dialog here where the thread has finished his work
progDialog.dismiss();
}
}
LE:
更多关于 AsyncTask 的细节 https://developer.android.com/reference/android/os/AsyncTask
特别检查受保护的方法
我正在开发一个 android 应用程序。 用户注册过程调用发送电子邮件的服务,因此需要几秒钟,例如 5 或 6 秒,这就是我在线程中执行该任务的原因。问题是,对话框永远不会消失。它保持滚动,用户无能为力。这是我的代码:
try
{
final ProgressDialog progDailog = new ProgressDialog(ActividadAltaUsuario.this);
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
URL url = new URL("slowWS");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = IOUtils.toString(in, "UTF-8");
final JSONObject jsonPrincipal = new JSONObject(response);
Boolean success = jsonPrincipal.get("status").toString() == "true";
if (success)
{
ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progDailog.show(ActividadAltaUsuario.this, "Sendind email");
}
});
final String idUsuario = jsonPrincipal.get("idUsuario").toString();
URL url2 = new URL("anotherSlowWS");
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
conn2.setRequestMethod("POST");
InputStream in2 = new BufferedInputStream(conn2.getInputStream());
String response2 = IOUtils.toString(in2, "UTF-8");
JSONObject jsonRtaMail = new JSONObject(response2);
//finish();
}
else
{
//finish();
showToast(jsonPrincipal.get("message").toString());
}
ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progDailog.dismiss();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection" + e.toString());
}
有人可以帮助我吗? 谢谢!
AsyncTask 将是一种比线程更好的方法,将线程中的网络调用替换为使用 AsyncTask。你可以使用这样的东西
private class LongOperation extends AsyncTask<Void, Void, Void> {
@Override
protected String doInBackground(Void... params) {
//Main stuff that needs to be done in background
}
@Override
protected void onPostExecute(Void result) {
//Post Execution this method will be called, handle result accordingly
//You can dismiss your dialog here
}
@Override
protected void onPreExecute() {
//Do initialization relative stuff here
// Initialize your dialog here.
}
}
由于 onPostExecute()
和 onPreExecute()
都在主线程上工作,您可以在此方法中显示和关闭对话框。
UI 控件只能从 UI 线程访问。 通常我在扩展 AsyncTask
的 class 中执行此操作类似于: public class MyTask 扩展了 AsyncTask {
protected void onPreExecute() {
//create and display your alert here
progDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Logging ...", true);
}
protected Void doInBackground(Void... unused) {
// here is the thread's work ( what is on your method run()
...
// if we want to show some progress in UI, then call
publishProgress(item)
}
protected void onProgressUpdate(Item... item) {
// theoretically you can show the progress here
}
protected void onPostExecute(Void unused) {
//dismiss dialog here where the thread has finished his work
progDialog.dismiss();
}
}
LE: 更多关于 AsyncTask 的细节 https://developer.android.com/reference/android/os/AsyncTask 特别检查受保护的方法