没有从NETWORK_PROVIDER获取更新

时间:2015-02-08 15:45:53

标签: android android-asynctask gps locationmanager locationlistener

我正在创建一个简单的位置应用程序,根据命令存储用户的位置。我正在尝试使用GPS提供商和网络提供商来建立用户的位置。

使用Asynctask实现该过程,其工作方式如下:

  1. 检查上次已知的GPS位置和网络位置。如果位置在可接受的参数范围内,则线程结束。
  2. 如果最后知道的位置不可接受,则需要更新GPS和网络。
  3. 一旦找到4个网络位置或找到3个GPS位置,线程就会完成。
  4. 等待位置修复时会显示一个对话框。

    问题:GPS似乎正在更新,而网络却没有。我试过单独更新NETWORK_PROVIDER,等了一个多小时没有结果。我试过开启和关闭WIFI。

    GetGPS类的代码:

    public class GetGPS extends AsyncTask<String, Void, String> implements LocationListener {
    
        private ProgressDialog progDialog;
        public double[] DataArray = {0.0,0.0,0.0};
        private Integer NetworkCount = 0;
        private Integer GPSCount = 0;
        private LocationManager mlocManager;       
        private Context mContext;
        private Activity mActivity;
        final LocationListener locationListener = (LocationListener) this;
        private boolean HasCompleted = false;
    
    
        public GetGPS(Context context, Activity activity) {
            mContext = context;
            mActivity = activity;   
        } 
    
    
        @Override
        public void onPreExecute() {
            Log.v(getClass().getName(),"PreExecute");
    
    
            mlocManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    
            //Stop any current GPS updates
            mlocManager.removeUpdates((LocationListener) this);
    
            //Get current system time in millis
            long currentTime = System.currentTimeMillis();
    
            //Check last known GPS location and corresponding accuracy and time
            String GPSlocationProvider = LocationManager.GPS_PROVIDER;
            Location lastKnownGPSLocation = mlocManager.getLastKnownLocation(GPSlocationProvider);
    
            if (lastKnownGPSLocation != null) {
                long lastGPStime = lastKnownGPSLocation.getTime();
                long timeDifference = currentTime - lastGPStime;
                float lastGPSaccuracy = lastKnownGPSLocation.getAccuracy();
                Log.v(getClass().getName(),"Found last known GPS! GPS time diff:"+timeDifference+", Accuracy:"+lastGPSaccuracy);
                if ((lastGPSaccuracy < 50) && (timeDifference < 180000)) {
                    Log.v(getClass().getName(),"Acceptable last known GPS location!");
                    DataArray[0] = (double) lastKnownGPSLocation.getLatitude(); 
                    DataArray[1] = (double) lastKnownGPSLocation.getLongitude(); 
                    DataArray[2] = (double) lastKnownGPSLocation.getAccuracy();
                    HasCompleted = true;
                }
                else {
                    Log.v(getClass().getName(),"Last known location GPS too inaccurate!");
                }   
            }
            else {
                Log.v(getClass().getName(),"No previous GPS locations!");
            }
    
            //Check last known network location and corresponding accuracy and time
            String NetworklocationProvider = LocationManager.NETWORK_PROVIDER;
            Location lastKnownNetworkLocation = mlocManager.getLastKnownLocation(NetworklocationProvider);
    
            if (lastKnownNetworkLocation != null) {
                long lastNetworktime = lastKnownNetworkLocation.getTime();
                long timeDifference = currentTime - lastNetworktime;
                float lastNetworkaccuracy = lastKnownNetworkLocation.getAccuracy();
                Log.v(getClass().getName(),"Found last known Network location! Time diff:"+timeDifference+", Accuracy:"+lastNetworkaccuracy);
                if ((lastNetworkaccuracy < 50) && (timeDifference < 180000)) {
                    Log.v(getClass().getName(),"Acceptable last known Network location!");
                    DataArray[0] = (double) lastKnownNetworkLocation.getLatitude(); 
                    DataArray[1] = (double) lastKnownNetworkLocation.getLongitude(); 
                    DataArray[2] = (double) lastKnownNetworkLocation.getAccuracy();
                    HasCompleted = true;
                }
                else {
                    Log.v(getClass().getName(),"Last known Network location too inaccurate!");
                }                   
            }
            else {
                Log.v(getClass().getName(),"No previous Network locations!");
            }
    
            // Request both providers to update
             mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,(LocationListener) this);
            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,(LocationListener) this);
    
    
            //Display dialog box
            progDialog = new ProgressDialog(mActivity);
            progDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Stop", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.v(getClass().getName(),"ProgDialog cancelled");
                    mlocManager.removeUpdates(locationListener);
                    GetGPS.this.cancel(true);
                }
            });
            progDialog.setMessage("Acquiring location..");
            progDialog.setIndeterminate(true);
            progDialog.setCancelable(false);
            progDialog.show();
            Log.v(getClass().getName(),"Show progdialog");
        }
    
    
       @Override
         public String doInBackground(String... params) {
             while (HasCompleted == false) {
                             }
             Log.v(getClass().getName(),"doInBackground completed");
             return "Success";
    
         }
         @Override
         public void onPostExecute(String result) {
             Log.v(getClass().getName(),"onPostExecute");
             //Destroy Dialog box
             progDialog.dismiss();
             //TODO: Deal with data
    
         }
         public void onLocationChanged(Location location) {
            Log.v(getClass().getName(),"onLocationChanged, Provider:'"+location.getProvider()+"'");
    
                // Check if from Network
                if (location.getProvider().equals("network")){
                    Log.v(getClass().getName(),"Network location Changed");
                    NetworkCount = NetworkCount + 1;
                    if (NetworkCount > 3) {
                        DataArray[0] = (double) location.getLatitude();
                         DataArray[1] = (double) location.getLongitude();
                         DataArray[2] = (double) location.getAccuracy();
                         Log.v(getClass().getName(),"Location stored from Network!");
                         Log.v("data array 0","" + DataArray[0]);
                         mlocManager.removeUpdates((LocationListener) this);
                         HasCompleted = true;
                    }
                    else {
                        Log.v(getClass().getName(),"Network location count:"+NetworkCount);
                    }
                } 
    
                //Check if from GPS
                if (location.getProvider().equals("gps")){
                    Log.v(getClass().getName(),"GPS location Changed");
                    GPSCount = GPSCount + 1;
                    if (GPSCount > 2) {
                         DataArray[0] = (double) location.getLatitude();
                         DataArray[1] = (double) location.getLongitude();
                         DataArray[2] = (double) location.getAccuracy();
                         Log.v(getClass().getName(),"Location stored from GPS!");
                         //Log.v("data array 0","" + DataArray[0]);
                         mlocManager.removeUpdates((LocationListener) this);
                         HasCompleted = true;
                    }
                    else {
                        Log.v(getClass().getName(),"GPS location count:"+GPSCount);
                    }
                } 
    
         }
    
            @Override
            public void onProviderDisabled(String provider) {
                Log.v("OnProviderDisabled", "OnProviderDisabled:"+provider);
            }
            @Override
            public void onProviderEnabled(String provider) {
                Log.v("onProviderEnabled", "onProviderEnabled:"+provider);
            }
            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                Log.v("onStatusChanged", "onStatusChanged, provider:"+provider+", Status:"+status);
    
            }
    
        }
    

    以下列方式调用GetGPS类:

    Context mContext = getActivity().getApplicationContext();
    Activity mActivity = getActivity();
    GetGPS getgps = new GetGPS(mContext, mActivity);
    getgps.execute();
    

    关于我哪里出错的任何帮助?提前感谢您的帮助。

    另外:是否有一种简单的方法来实现Asynctask的超时?即如果在设定的时间段后没有找到位置修复,则线程停止。

1 个答案:

答案 0 :(得分:0)

关于您的第一个问题,请检查您的手机是否启用了GPS和网络提供商:
enter image description here

我也不确定在AsyncTask中实现LocationListener是否正确,我试着阅读它并遇到this。尝试阅读并实施他提出的解决方案。 (我要做的是创建一个独立的位置Singelton类,它将具有停止和启动等方法,并将从AsyncTask控制。)

在你的doInBackground方法中,我会改变While循环,只需将Thread.sleep(50)放入其中。以防止忙碌的等待。

关于你的第二个问题,这里最简单的方法是检查doInBackground的while循环中经过的时间并停止必要的线程。