使用asynctask进度条保持为空 - 编辑 - 对话框永远不会停止加载

时间:2013-05-16 08:27:16

标签: android

我希望在等待GPS信号建立时有一个进度条。

我用:

public class GetGPSData extends AsyncTask<Void, Integer, Void> {
               @Override
               protected void onPreExecute() {
                   //super.onPreExecute();
                   myprogressBar.setVisibility(View.VISIBLE);
                   myprogressBar.setProgress(0);

               }

               @Override
               protected void onProgressUpdate(Integer... progress) {
                   //super.onProgressUpdate(progress);
                   myprogressBar.setProgress(progress[0]);

               }

               @Override
               protected Void doInBackground(Void... params) {

               latitude = gps.getLatitude();
               longitude = gps.getLongitude();

               while (latitude == 0 || longitude ==0)
               {

                   try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
               }
                return null;
               }

                  protected void onCancelled() {
                      Toast.makeText(getBaseContext(), "Error connecting", Toast.LENGTH_LONG).show();

                  }

               @Override
               protected void onPostExecute(Void result) {
                   super.onPostExecute(result);
                   myprogressBar.setProgress(100);

               Toast.makeText(getBaseContext(), "Your Location is  \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

               }
           }

 <ProgressBar android:id="@+id/myprogressBar"
  style="?android:attr/progressBarStyleHorizontal" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="true"
  android:indeterminate="true"
  android:layout_below="@+id/comments"
  android:padding="5dp" />

但进度条仍为空。

此外,当我点击按钮以开始获取GPS信号时,GPS会闪烁,然后停止闪烁(但同时它仍会搜索信号)当我再次按下它时它会给我Toast消息I有onPostExecute。这是正常行为吗?

图标是否闪烁,直到找到信号然后显示消息而用户不必再次按下按钮?

-------------------------------- UPDATE -------------- --------------------------------------

我也尝试过ProgressDialog:

public class GetGPSData extends AsyncTask<Void, Integer, Void> {
               ProgressDialog progressDialog = null;               

               @Override
               protected void onPreExecute() {
                  super.onPreExecute();



                   progressDialog = new ProgressDialog(ShowList.this);
                   progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            GetGPSData.this.cancel(true);
                        }

                    });
                   progressDialog.setMessage("Waiting for location...");
                   progressDialog.setIndeterminate(true);
                   progressDialog.setCancelable(true);
                   progressDialog.show();


               }

               @Override
               protected void onProgressUpdate(Integer... progress) {
                   super.onProgressUpdate(progress);


               }

               @Override
               protected Void doInBackground(Void... params) {

                   latitude = gps.getLatitude();
                   longitude = gps.getLongitude();


              while  (latitude == 0 || longitude == 0)
               {
                   try {               
                    Thread.sleep(1000);     

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  

               }


                return null;
               }

                  protected void onCancelled() {
                      Toast.makeText(getBaseContext(), "Cancelled/Error connecting", Toast.LENGTH_LONG).show();
                      progressDialog.dismiss();
                  }

               @Override
               protected void onPostExecute(Void result) {

                   progressDialog.dismiss();

 Toast.makeText(ShowList.this, "Your Location is  \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

               }
           }

但是现在进度对话框永远不会停止加载。如果我按下后退按钮然后再按下按钮来获取位置,那么它会给我一个toast消息(带位置)。

------------------- SOLUTION --------------------------- ---

问题是(纠正while循环后)你必须把this.location = location

 public void onLocationChanged(Location location) {
        this.location=location;
    }

4 个答案:

答案 0 :(得分:3)

doInBackground方法

中使用publishProgress

但在你的情况下,你不应该使用水平ProgressBar值,因为你不知道你的经度和纬度何时知道,只需使用一个简单的ProgressBar

答案 1 :(得分:2)

您的进度条为空,因为您没有在doInBackground()方法中调用 publishProgress()方法。 publishProgress在你的asynctask中调用onProgressUpdate()方法。

答案 2 :(得分:1)

您的进度未发布的原因是因为您未在doInBackground函数中调用publishProgress()。此外,GPS图标停止闪烁的原因是因为您有GPS信号。您的应用程序说它仍在搜索GPS信号的原因是因为您的while循环条件永远不会更新,因此您的循环永远不会结束。当您再次按下按钮时,吐司会显示,因为正在启动第二个AsyncTask并且它立即完成。

要解决所有这些问题,您需要更新doInBackground函数,使其如下所示:

@Override
protected Void doInBackground(Void... params) {

    do {
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

        /* Not needed for an indeterminate progress bar */
        publishProgress(0);

        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    } while  (latitude == 0 || longitude == 0);

    return null;
}

这将持续寻找gps修复,并在实现修复时正确地突破循环。它还会致电onProgressUpdate来更新您的进度。这里唯一需要注意的是,您需要自己确定将要用作进度的内容。我在这里只是为0,所以你可以看到在哪里调用它但不知道你用什么来衡量进度,我无法明确地说出最适合你的方法。

在您的情况下,不确定的进度条可能比具有最小值/最大值的进度条更好。我在你的ProgressDialog示例中看到你已经完成了这一操作,因此我只需删除上述代码中的publishProgress行,然后再使用您的第二个示例。

答案 3 :(得分:1)

当doInBackground回调运行结束时,将调用onPostExecute回调。如果没有被调用则意味着

 while  (latitude == 0 || longitude == 0) {
        try {               
                Thread.sleep(1000);     

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  

       latitude = gps.getLatitude();
       longitude = gps.getLongitude();

  }

 Log.e(getClass().getSimpleName(), "out the while");

latitudelongitude值均不是!= 0;

如果您的while循环永不结束,您将泄露AsyncTask