我正在使用一个需要后台处理的应用程序,如下所示。
每当我将请求发送到URL时,我将以JSON格式获得输出。在JSON中我有一个布尔变量。如果布尔变量是true
,我再次需要将请求发送到URL,直到我得到布尔变量的值为false
。
但是在前台,SplashScreen应该与Progress Bar一起运行。我怎样才能做到这一点。
任何建议?
答案 0 :(得分:1)
当进度条在ui线程上运行时,使用aynctask或单独的线程获取服务器上的数据。
答案 1 :(得分:1)
您可以使用AsyncTask<>
来实现此目的。您可以在以下网址了解详情:http://www.vogella.com/articles/AndroidPerformance/article.html
你的想法可以是这样的:
onCreate()
内启动AsyncTask以获取JSON,并在AsyncTask的onProgressUpdate()
函数中更新进度条。
onPostExecute(..)
]时,调用一个函数来解析JSON,并检查true
/ false
值true
则再次重复该任务。您也可以使用自定义ProgressDialog
。
答案 2 :(得分:1)
使用AsyncTask,这是一个非常强大的工具,但可能比其他技术更复杂...... AsynchTask使用泛型类型。有一种方法需要实施。
protected T doInBackground(T... params) {
// body of your method
}
和其他方法。一般
protected T doInBackground {}
protected void onProgressUpdate(T... params)
protected void onPostExecute(T param)
First 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();
}
}
希望它有所帮助!