我正在處理一個需要後臺處理的應用程序,如下所示。如何在Android中的SplashScreen的後臺運行代碼
每當我發送請求到URL我會得到JSON格式的輸出。在JSON中我有一個布爾變量。如果布爾變量是true
,我又需要將請求發送到URL,直到我得到布爾變量的值爲false
。
但是在前景中,SplashScreen應該與進度條一起運行。我怎樣才能做到這一點。
有什麼建議嗎?
我正在處理一個需要後臺處理的應用程序,如下所示。如何在Android中的SplashScreen的後臺運行代碼
每當我發送請求到URL我會得到JSON格式的輸出。在JSON中我有一個布爾變量。如果布爾變量是true
,我又需要將請求發送到URL,直到我得到布爾變量的值爲false
。
但是在前景中,SplashScreen應該與進度條一起運行。我怎樣才能做到這一點。
有什麼建議嗎?
使用AsyncTask,這是非常強大的工具,但可能更復雜像其他技術... AsynchTask使用泛型類型。有一種方法必須實現。
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();
}
}
希望它有幫助!
當進度條在ui線程上運行時,使用aynctask或單獨的線程在服務器上獲取數據。
您可以使用AsyncTask<>
來實現此目的。以進度條
你的想法的流動可以是這樣的。
onCreate()
啓動您的AsyncTask來獲取JSON,並更新您的AsyncTask的onProgressUpdate()
函數的進度條。
onPostExecute(..)
],調用一個函數來解析JSON,並檢查true
/false
值true
然後再次重複任務。您也可以使用定製的ProgressDialog
。