0

一部の情報をフェッチするコードを 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();
    }
}

私はログイン活動を行っており、オンラインサーバーから登録ユーザー情報を取得しています..

4

2 に答える 2

1

さて、ここにあるのは、UI スレッドで実行される runnable をすぐに作成する AsyncTask であり、AsyncTask の目的を完全に無効にします。runOnUIThread を取り除き、バックグラウンド コールですべての処理を行い、OnProgressUpdate と OnPostExecute を使用して UI を更新します。また、ここで API ドキュメントを準備します。

http://developer.android.com/reference/android/os/AsyncTask.html

ProgressDialog は自動的に更新されません。AsyncTask からの OnProgressUpdate 呼び出しで更新する必要があります。

于 2012-07-24T21:05:46.643 に答える
0

はい、 からdoInBackgroundに渡す整数を指定してpDialog.setProgress(int) publishProgressを呼び出しますAsyncTask署名を変更して整数を渡し<Void, Integer, Void> ます に渡す前に、その進捗整数を 0 ~ 10000 の間で正規化する必要があります。onProgressUpdate これは、バイトまたは行をループし、チャンクで読み取り、進行状況の更新を渡します。

変更する必要があるもう 1 つのことは、 Toast のdoInBackground ようなものを移動する必要がある間、UI 内の任意の方法でオブジェクトにアクセス/変更/タッチできないことです。また、Activity メソッドも (つまり)メソッドに移動する必要があります。alert.show()finish()postExecute

于 2012-07-24T21:40:00.403 に答える