在我通过eclipse重新安装apk文件后,getlastknownlocation始终返回null

Stu*_*ent 24 gps android location

最近,我创建了一个简单的应用程序来获取gps位置并在Android手机上显示.开始时,我可以在几次尝试后获取位置,但在重新安装apk文件后,getLastKnownLocation()始终返回空值.

使用的开发环境: - API 10姜饼2.3.6 - 使用GPS提供商

下面是我在我的android项目中应用的代码:

        public class MyActivity extends MapActivity{
protected void onCreate(Bundle savedInstanceState) {


    mapView = (MapView)findViewById(R.id.myTripMap);
    mapController = mapView.getController();
    mapView.setSatellite(false);
    mapView.setStreetView(true);
    mapView.displayZoomControls(false);
    mapView.setBuiltInZoomControls(true);//
    mapView.setClickable(true);
    mapController.setZoom(14);      


    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(provider);

    updateMyCurrentLoc(location);

    locationManager.requestLocationUpdates(provider, 2, 1,locationListener);



}


          private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          updateMyCurrentLoc(location);
        }

        public void onProviderDisabled(String provider){
          updateMyCurrentLoc(null);
        }

        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status, 
                                    Bundle extras){ }
      };


      private void updateMyCurrentLoc(Location location) {



            if (location != null) {

             // other codes to get the address and display
            Toast.makeText(getBaseContext(), "provider used : "+provider).show();   //testing purpose
            } 
            else {
              str = "No location found";
              Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议一个可能的解决方案来解决getLastKnownLocation()返回的null值吗?任何帮助将不胜感激.谢谢.

kar*_*ran 45

getLastKnownLocation()此返回最后一个已知的位置......你以后重新安装应用程序不会有任何最后的已知位置...所以它会导致NPE ...而不是使用下面的代码

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
Run Code Online (Sandbox Code Playgroud)

它对我有用......也应该为你工作......

  • 这不是那么好的代码..将所有代码放在try/catch中,而没有任何明确需要它,原则上是错误的编程.如果你真的想要一个很棒的代码,请试试这个:https://web.archive.org/web/20100429083435/http://marakana.com/forums/android/android_examples/42.html (18认同)
  • 这在很多层面都是错的,我不知道怎么说.首先,getLastKnownLocation返回设备的最后已知位置,而不是应用程序.如果设备尚未跟踪该位置,它将返回null.所以你的代码仍然可以返回null,并且会定期执行.该代码中还有十几个其他错误作为奖励. (9认同)
  • 在我的情况下,即使GPS启用也无法从GPS_PROVIDER获取位置.每次从NETWORK_PROVIDER获取.任何的想法 ??谁能帮我吗. (5认同)
  • getLastKnownLocation仍然返回null!没有为我工作 (4认同)

小智 -1

您只需在代码中再添加一行即可

if (locationManager != null) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();

    } else {
        Toast.makeText(
                mContext,
                "Location Null", Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)