一部の情報をフェッチするコードを Async クラスに転送しました。これは、バッファリングを最小限に抑え、「応答しない」、またはアプリケーションのさらに悪いクラッシュの可能性を回避するためです。
ここで、ProgressDialog を表示したいのですが、ユーザーが接続を中断しないように「キャンセル不可」に設定する必要があります。
しかし、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();
}
}
私はログイン活動を行っており、オンラインサーバーから登録ユーザー情報を取得しています..