私は次のようにバックグラウンド処理を必要とする1つのアプリケーションで作業しています。
URLにリクエストを送信するたびに、JSON形式で出力を取得します。JSONには、ブール変数があります。ブール変数がtrue
、の場合、ブール変数の値を。として取得するまで、URLにリクエストを送信する必要がありますfalse
。
ただし、フォアグラウンドでは、SplashScreenはプログレスバーと一緒に実行する必要があります。どうすればこれを達成できますか。
助言がありますか?
私は次のようにバックグラウンド処理を必要とする1つのアプリケーションで作業しています。
URLにリクエストを送信するたびに、JSON形式で出力を取得します。JSONには、ブール変数があります。ブール変数がtrue
、の場合、ブール変数の値を。として取得するまで、URLにリクエストを送信する必要がありますfalse
。
ただし、フォアグラウンドでは、SplashScreenはプログレスバーと一緒に実行する必要があります。どうすればこれを達成できますか。
助言がありますか?
プログレスバーがUIスレッドで実行されている間に、aynctaskまたは別のスレッドを使用してサーバー上のデータをフェッチします。
これを達成するために使用できますAsyncTask<>
。詳細については、 http: //www.vogella.com/articles/AndroidPerformance/article.htmlをご覧ください。
アイデアの流れは次のようになります。
onCreate()
でAsyncTaskを開始してJSONをフェッチし、onProgressUpdate()
AsyncTaskの機能でプログレスバーを更新します。
onPostExecute(..)
]、関数を呼び出してJSONを解析し、true
/false
値を確認しますtrue
タスクをもう一度繰り返します。ProgressDialog
カスタマイズもご利用いただけます。
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();
}
}
それが役に立てば幸い!