我将获取一些信息的代码转移到Async类中,这是为了最大限度地减少缓冲并避免“不响应”的可能性,或者更糟糕的应用程序崩溃。
现在,我想显示一个ProgressDialog,它应该被设置为'not cancelable'以避免用户中断连接。
但是如果我的ProgressDialog确实显示了我的应用程序中实际发生的进展(在从在线服务器获取信息期间),我有点困惑。因为在我的模拟器上,我的应用程序暂停(大约2秒钟),然后显示ProgressDialog,然后立即显示“获取”的结果。
非常感谢任何帮助。
感谢。这是我的代码。
public void onClick(View v)
{
new retrieveOnline().execute();
}
private class retrieveOnline extends AsyncTask <Void, Void, Void>
{
protected void onPreExecute ()
{
pDialog = new ProgressDialog (FirstScreen.this);
pDialog.setMessage("Please wait...");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... unused)
{
runOnUiThread (new Runnable()
{
public void run()
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mysite.com/database/login.php");
stringEmail = etEmail.getText().toString();
stringPassword = etPassword.getText().toString();
try
{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stringEmail", stringEmail));
nameValuePairs.add(new BasicNameValuePair("stringPassword", stringPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()== 200)
{
entity = response.getEntity();
if(entity != null)
{
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String errorEmail = jsonResponse.getString("errorEmail");
if (errorEmail != "")
{
tvEmailError.setText(errorEmail);
}else{}
String errorPassword = jsonResponse.getString("errorPassword");
if (errorPassword != "")
{
tvPasswordError.setText(errorPassword);
}else{}
String inactiveAccount = jsonResponse.getString("inactiveAccount");
if (inactiveAccount.length() != 0)
{
AlertDialog alert = new AlertDialog.Builder(FirstScreen.this).create();
alert.setCancelable(false);
alert.setMessage("Your account is currently inactive and unusable." + "\nDo you want to send an account activation message to your email now?");
alert.setButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mysite.com/database/activate2.php");
stringEmail = etEmail.getText().toString();
stringPassword = etPassword.getText().toString();
try
{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stringEmail", stringEmail));
nameValuePairs.add(new BasicNameValuePair("stringPassword", stringPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()== 200)
{
entity = response.getEntity();
if(entity != null)
{
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String successActivation = jsonResponse.getString("successActivation");
if (successActivation.length() != 0)
{
//Progress Dialog here.
Toast.makeText(getBaseContext(), "We successfully sent an activation message to your email account. Try to log in again after activating your account.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, we are unable to reach your email account.",Toast.LENGTH_SHORT).show();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(), "Connection to the server is lost. Please check your internet connection.", Toast.LENGTH_SHORT).show();
}
}
});
alert.setButton2("Not now", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
AlertDialog alert2 = new AlertDialog.Builder(FirstScreen.this).create();
alert2.setCancelable(false);
alert2.setMessage("Exit FindDroid?");
alert2.setButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
}
});
alert2.setButton2("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
//Do nothing
}
});
alert2.show();
}
});
alert.show();
}else{}
if ((errorEmail.length()==0) && (errorPassword.length()==0)&& (inactiveAccount.length()==0))
{
String dbEmail = jsonResponse.getString("dbEmail");
String dbPassword = jsonResponse.getString("dbPassword");
//---Store dbEmail and dbPassword to SharedPreferences---//
//-------------------------------------------------------//
Intent i = new Intent(getApplicationContext(), Construction.class);
startActivity(i);
finish();
}
}//if (entity!=null)..
}//if response()...
}//try..
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(), "Connection to the server is lost. Please check your internet connection.", Toast.LENGTH_SHORT).show();
}
}
});
return (null);
}
protected void onPostExecute (Void unused)
{
pDialog.dismiss();
}
}
我正在进行登录活动,它正在从在线服务器获取注册用户信息..
答案 0 :(得分:1)
好的,所以你在这里有一个AsyncTask,它立即创建一个在UI线程上运行的runnable,完全违背了AsyncTask的目的。摆脱runOnUIThread,在后台调用中执行所有处理,并使用OnProgressUpdate和OnPostExecute更新UI。还准备好API文档:
http://developer.android.com/reference/android/os/AsyncTask.html
ProgressDialog不会自动更新,您需要在AsyncTask的OnProgressUpdate调用中更新它。
答案 1 :(得分:0)
是的,从doInBackground
调用publishProgress并使用整数传递给pDialog.setProgress(int)
更改AsyncTask
签名以传递整数<Void, Integer, Void>
您需要规范化该进度整数在传递给onProgressUpdate
之前0-10000之间这可以通过循环遍历字节或行,从块中读取图像或大量数据,读取块以及随时传递进度更新来完成。
您需要修改的另一件事是,您在doInBackground
期间无法访问/修改/触摸任意方式的对象您需要移动alert.show()
Toast和I之类的内容也将活动方法(即finish()
)与postExecute
方法相提并论。