Android - 带有 Web 服务 AsyncTask 的 Slow ProgressDialog

发布于 2024-12-10 13:32:24 字数 1495 浏览 0 评论 0原文

我有一个问题。我有一个异步任务,在其中我连接到网络服务,引入数据并将其放入数据库中。没关系,它可以工作。但我有一个问题,我想在单击“刷新”后放置一个进度条(即当我执行网络服务时)。

当我刷新时,屏幕冻结需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-12-17 13:32:24

尝试将实例化 ProgressDialog 的方式更改为:

     @Override
     protected void onPreExecute() {
         pd  = ProgressDialog.show( context, "", "My text is here..." );
     }

请注意,如果您仅使用模拟器进行测试,ProgressDialogs 的出现/消失可能会很慢,这意味着如果您在设备上进行测试,您的问题可能不存在。

Try changing the way you instantiate your ProgressDialog to this:

     @Override
     protected void onPreExecute() {
         pd  = ProgressDialog.show( context, "", "My text is here..." );
     }

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.

临风闻羌笛 2024-12-17 13:32:24

我建议您在您的环境 Activity 中声明 ProgressDialog。然后你可以尝试这个代码。它对我有用:

声明 AsyncTask 的环境类:

private ProgressDialog dialog;

...

private class tareaActualizar extends AsyncTask<Void, Void, Boolean> {
         private static final String DEBUG_TAG = "actualizo";

...
@Override
         protected void onPreExecute() {

                    pd = new ProgressDialog(ShamanOperativoActivity.this);

            pd = ProgressDialog.show(getParent().getParent(), "Wait",
                    "Loading...");


         }
...

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:

private ProgressDialog dialog;

...

private class tareaActualizar extends AsyncTask<Void, Void, Boolean> {
         private static final String DEBUG_TAG = "actualizo";

...
@Override
         protected void onPreExecute() {

                    pd = new ProgressDialog(ShamanOperativoActivity.this);

            pd = ProgressDialog.show(getParent().getParent(), "Wait",
                    "Loading...");


         }
...
西瓜 2024-12-17 13:32:24

试试这些......

private class tareaActualizar extends AsyncTask<Void, Void, Void> 

{

private static final String DEBUG_TAG = "actualizo";

         private ProgressDialog pd = new ProgressDialog(ShamanOperativoActivity.this);

        protected void onPreExecute() {
              pd.setMessage("Actualizando...");
             pd.show();

        }

        protected Void doInBackground(Void... para) {

              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;


        }

        protected void onPostExecute(Void params) {

              pd.dismiss();

        }
    }

Try these one .....

private class tareaActualizar extends AsyncTask<Void, Void, Void> 

{

private static final String DEBUG_TAG = "actualizo";

         private ProgressDialog pd = new ProgressDialog(ShamanOperativoActivity.this);

        protected void onPreExecute() {
              pd.setMessage("Actualizando...");
             pd.show();

        }

        protected Void doInBackground(Void... para) {

              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;


        }

        protected void onPostExecute(Void params) {

              pd.dismiss();

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文