1

私は次のようにバックグラウンド処理を必要とする1つのアプリケーションで作業しています。

URLにリクエストを送信するたびに、JSON形式で出力を取得します。JSONには、ブール変数があります。ブール変数がtrue、の場合、ブール変数の値を。として取得するまで、URLにリクエストを送信する必要がありますfalse

ただし、フォアグラウンドでは、SplashScreenはプログレスバーと一緒に実行する必要があります。どうすればこれを達成できますか。

助言がありますか?

4

3 に答える 3

1

プログレスバーがUIスレッドで実行されている間に、aynctaskまたは別のスレッドを使用してサーバー上のデータをフェッチします。

于 2012-04-23T11:46:24.720 に答える
1

これを達成するために使用できますAsyncTask<>詳細については、 http: //www.vogella.com/articles/AndroidPerformance/article.htmlをご覧ください。

アイデアの流れは次のようになります。

  1. プログレスバーでスプラッシュアクティビティを開始します。
  2. 内部onCreate()でAsyncTaskを開始してJSONをフェッチし、onProgressUpdate()AsyncTaskの機能でプログレスバーを更新します。
    • AsyncTaskが完了したら[ onPostExecute(..)]、関数を呼び出してJSONを解析し、true/false値を確認します
    • その後、trueタスクをもう一度繰り返します。
    • falseの場合、AsyncTaskを停止します。
  3. これでURLフェッチタスクが完了し、次のアクティビティに進むことができます。

ProgressDialogカスタマイズもご利用いただけます。

于 2012-04-23T11:59:43.950 に答える
1

AsyncTaskを使用します。これは非常に強力なツールですが、他の手法のようにもっと複雑かもしれません...AsynchTaskはジェネリック型を使用します。実装する必要のあるメソッドが1つあります。

protected T doInBackground(T... params) {
   // body of your method
}

およびその他の方法。いつもの

protected T doInBackground {}
protected void onProgressUpdate(T... params)
protected void onPostExecute(T param)

最初のTは、いくつかの入力データに使用されます。あなたの場合、それは例えばあなたのURLである可能性があります。このメソッド(doInBackground)は、私の上の同僚が書いた方法でバックグラウンドスレッドで実行されており、おそらくすべてを書いています。似たような例はほとんどありません。

private class DownloadAsyncTask extends AsyncTask<String, DataTransmitted, InputStream> {
      protected InputStream doInBackground(String... params) {

            int contentLength = 0;
            int buffLength = 0;
            int progress = 0;
            InputStream inputStream = null;
            try {

                URL url = new URL(params[0]);
                HttpURLConnection urlConntection = (HttpURLConnection) url.openConnection();
                urlConntection.setRequestMethod("GET");
                urlConntection.setAllowUserInteraction(false);
                urlConntection.setInstanceFollowRedirects(true);
                urlConntection.connect();
                if (urlConntection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    contentLength = urlConntection.getContentLength();
                    progressDialog.setMax(contentLength);
                    inputStream = urlConntection.getInputStream();

                    while ((buffLength = inputStream.read(MAX_BUFFER)) != -1) {
                        try {
                            Thread.sleep(1);
                            progress += buffLength;
                            DataTransmitted data = new DataTransmitted(progress, contentLength);
                            publishProgress(data);
                        }
                        catch (InterruptedException ex) {
                            Log.e(this.getClass().getName(), ex.getMessage());
                        }
                    }

                }
                else {
                    throw new IOException("No response.");
                }

            }
            catch (MalformedURLException ex) {
                Log.e(this.getClass().getName(), ex.getMessage());
            }
            catch (IOException ex) {
                Log.e(this.getClass().getName(), ex.getMessage());
            }
            return inputStream;
        }

      @Override
        protected void onProgressUpdate(DataTransmitted... data) {

            progressDialog.setProgress(data[0].getProgress());
            progressDialog.setMessage(data[0].getProgress() + " bytes downloaded.");
            data[0] = null;
        }


        @Override
        protected void onPostExecute(InputStream inStream) {
            progressDialog.dismiss();
            progressDialog.setProgress(0);
            DownloadedStream.inputStream = inStream;
            DownloadedStream.test = "Toto je len test...";
            Thread.currentThread().interrupt();
            downloadTask = null;
            new AlertDialog.Builder(DownloadPictureActivity.this)
                .setTitle("Download finished")
                .setMessage("Picture was downloaded correctly.")
                .setPositiveButton("Zobraziť", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dlg, int whichButton) {
                        // body of method

                    }
                })
                .setNegativeButton("Zavrieť", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dlg, int whichButton) {
                     //body of method   
                    } 
                })
                .show();
        }
}

それが役に立てば幸い!

于 2012-04-23T12:36:49.553 に答える