Android - 带有 Web 服务 AsyncTask 的 Slow ProgressDialog
我有一个问题。我有一个异步任务,在其中我连接到网络服务,引入数据并将其放入数据库中。没关系,它可以工作。但我有一个问题,我想在单击“刷新”后放置一个进度条(即当我执行网络服务时)。
当我刷新时,屏幕冻结需要 2 秒,然后稍微启动进度条,然后然后消失,但任务在进度条开始之前完成。
这是asynctask Webservice的代码
private class tareaActualizar extends AsyncTask<Void, Void, Boolean> {
private static final String DEBUG_TAG = "actualizo";
private ProgressDialog pd = new ProgressDialog(ShamanOperativoActivity.this);
@Override
protected void onCancelled() {
Log.i(DEBUG_TAG, "onCancelled");
pd.dismiss();
}
@Override
protected void onPostExecute(Boolean result) {
Log.i(DEBUG_TAG, "onPostExecute");
pd.dismiss();
}
@Override
protected void onPreExecute() {
pd.setMessage("Actualizando...");
pd.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
@Override
protected Boolean doInBackground(Void... params) {
webService();
tv2 = resultado.toString(); //Obtengo el string resultado del WebService en tv2
//Obtengo la hora actual
insertoEnBD();
// Inserto en base de datos (Si es que pase por el Web Service)
Boolean res = true;
return res;
}
}
是Webservice的连接方法,“InsertoBD”是当我将数据放入数据库时..谢谢
i have a problem. I have an asynctask, in which i connect to a web service, bring data and put it in a database. Thats ok, it works. But i have a problem, i want to put a progress bar after i click REFRESH (thats when i do the web service)..
When i refresh, it takes 2 seconds while my screen is freezed and after that starts the progressbar a little and then dissapear, but the taks is done before the progressbar starts.
Here is the code of the asynctask
private class tareaActualizar extends AsyncTask<Void, Void, Boolean> {
private static final String DEBUG_TAG = "actualizo";
private ProgressDialog pd = new ProgressDialog(ShamanOperativoActivity.this);
@Override
protected void onCancelled() {
Log.i(DEBUG_TAG, "onCancelled");
pd.dismiss();
}
@Override
protected void onPostExecute(Boolean result) {
Log.i(DEBUG_TAG, "onPostExecute");
pd.dismiss();
}
@Override
protected void onPreExecute() {
pd.setMessage("Actualizando...");
pd.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
@Override
protected Boolean doInBackground(Void... params) {
webService();
tv2 = resultado.toString(); //Obtengo el string resultado del WebService en tv2
//Obtengo la hora actual
insertoEnBD();
// Inserto en base de datos (Si es que pase por el Web Service)
Boolean res = true;
return res;
}
}
Webservice is the method of the conection of webservice, and "InsertoBD" is when i put data into database.. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将实例化 ProgressDialog 的方式更改为:
请注意,如果您仅使用模拟器进行测试,ProgressDialogs 的出现/消失可能会很慢,这意味着如果您在设备上进行测试,您的问题可能不存在。
Try changing the way you instantiate your ProgressDialog to this:
Note, that if you only use the emulator for testing, ProgressDialogs can be slow in appearing/disappearing meaning your problem might not exist if you test on a device.
我建议您在您的环境 Activity 中声明 ProgressDialog。然后你可以尝试这个代码。它对我有用:
声明 AsyncTask 的环境类:
I suggest you to declare the ProgressDialog in your environment Activity. And then you can try this code. It works for me:
Environment class where AsyncTask is declared:
试试这些......
Try these one .....