编辑 - - 对话从未停止加载使用AsyncTask的进度栏保持空白进度、加载、从未、空白

2023-09-13 02:17:37 作者:人走茶亦凉

我想有一个进度条等待GPS信号来确定。

我使用的:

 公共类GetGPSData扩展的AsyncTask<太虚,整型,太虚> {
               @覆盖
               在preExecute保护无效(){
                   //super.on$p$pExecute();
                   myprogressBar.setVisibility(View.VISIBLE);
                   myprogressBar.setProgress(0);

               }

               @覆盖
               保护无效onProgressUpdate(整数...进度){
                   //super.onProgressUpdate(progress);
                   myprogressBar.setProgress(进展[0]);

               }

               @覆盖
               保护无效doInBackground(虚空...... PARAMS){

               纬度= gps.getLatitude();
               经度= gps.getLongitude();

               而(纬度== 0 ||经度== 0)
               {

                   尝试 {
                    视频下载(3000);
                }赶上(InterruptedException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
               }
                返回null;
               }

                  保护无效onCancelled(){
                      Toast.makeText(getBaseContext(),错误连接,Toast.LENGTH_LONG).show();

                  }

               @覆盖
               保护无效onPostExecute(无效的结果){
                   super.onPostExecute(结果);
                   myprogressBar.setProgress(100);

               Toast.makeText(getBaseContext(),您现在的位置是\新工人艺术团:+纬度+\ nLong:+经度,Toast.LENGTH_LONG).show();

               }
           }
 

 <进度机器人:ID =@ + ID / myprogressBar
  风格=机器人:ATTR / progressBarStyleHorizo​​ntal
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:layout_alignParentLeft =真
  机器人:不确定=真
  机器人:layout_below =@ + ID /评论
  机器人:填充=5DP/>
 

但进度条仍然是空的。

此外,当我点击才能开始获取GPS信号的按钮,GPS闪烁,然后停止闪烁(但同时仍然会搜索信号),当我preSS一遍它给了我吐司消息我在onPostExecute.Is这是正常的行为呢?

不应图标闪烁,直到找到信号,然后告诉我,而用户不必preSS再按下此键的消息?

--------------------------------更新-------------- --------------------------------------

我也试过用ProgressDialog:

 公共类GetGPSData扩展的AsyncTask<太虚,整型,太虚> {
               ProgressDialog progressDialog = NULL;

               @覆盖
               在preExecute保护无效(){
                  super.on preExecute();



                   progressDialog =新ProgressDialog(ShowList.this);
                   progressDialog.setOnCancelListener(新DialogInterface.OnCancelListener(){
                        @覆盖
                        公共无效OnCancel的(DialogInterface对话){
                            GetGPSData.this.cancel(真正的);
                        }

                    });
                   progressDialog.setMessage(等待位置......);
                   progressDialog.setIndeterminate(真正的);
                   progressDialog.setCancelable(真正的);
                   progressDialog.show();


               }

               @覆盖
               保护无效onProgressUpdate(整数...进度){
                   super.onProgressUpdate(进度);


               }

               @覆盖
               保护无效doInBackground(虚空...... PARAMS){

                   纬度= gps.getLatitude();
                   经度= gps.getLongitude();


              而(纬度== 0 ||经度== 0)
               {
                   尝试 {
                    视频下载(1000);

                }赶上(InterruptedException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }

               }


                返回null;
               }

                  保护无效onCancelled(){
                      Toast.makeText(getBaseContext(),取消/错误连接,Toast.LENGTH_LONG).show();
                      progressDialog.dismiss();
                  }

               @覆盖
               保护无效onPostExecute(无效的结果){

                   progressDialog.dismiss();

 Toast.makeText(ShowList.this,您现在的位置是\新工人艺术团:+纬度+\ nLong:+经度,Toast.LENGTH_LONG).show();

               }
           }
 
win7 旗舰版 64位,求解啊啊啊啊

但现在的进度对话框永远不会停止loading.If我preSS后退按钮,然后再按下此键来获得位置,然后它给我敬酒消息(与位置)。

-------------------解决方案--------------------------- ---

但问题是(修正后的while循环),您必须把this.location =位置

 公共无效onLocationChanged(位置定位){
        this.location =位置;
    }
 

解决方案

在doInBackground回调运行到结束onPostExecute回调函数。如果它不叫这意味着

 而(纬度== 0 ||经度== 0){
        尝试 {
                视频下载(1000);

            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

       纬度= gps.getLatitude();
       经度= gps.getLongitude();

  }

 Log.e(的getClass()getSimpleName(),出,而。);
 

没有纬度经度值为 = 0;!

另外,如果你的,而循环永远不会结束,你会泄露你的的AsyncTask

I want to have a progress bar while waiting for GPS signal to establish.

I use:

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();

               }
           }

and:

 <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" />

But the progress bar remains empty.

Also,when I click the button in order to start getting the GPS signal, the GPS flashes ,then stop flashing ( but in the meanwhile it still searches for signal ) and when I press it again it gives me the Toast message I have in onPostExecute.Is this normal behavior?

Shouldn't the icon be flashing until finds the signal and then show me the message without the user having to press again the button?

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

I also tried with 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();

               }
           }

but now the progress dialog never stops loading.If I press back button and then again the button to get location ,then it gives me the toast message (with location).

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

The problem was (after correcting the while loop) that you must put this.location=location

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

解决方案

the onPostExecute callback is invoked when the doInBackground callback runs to end. If it is not called it means that

 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");

neither latitude or longitude value is != 0;

Also if your while loop never ends you will leak your AsyncTask