有时候我的位置空了,直到我重启手机为什么?

时间:2014-03-02 08:48:42

标签: android location

所以我得到了经度和纬度:

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

         // Define a listener that responds to location updates
         locationListener = new LocationListener() {
             public void onLocationChanged(Location location) {
               // Called when a new location is found by the network location provider.
               longitude=location.getLongitude();
               latitude=location.getLatitude();

             }


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

             public void onProviderEnabled(String provider) {}

             public void onProviderDisabled(String provider) {}
           };

         //Or use LocationManager.GPS_PROVIDER
         String locationProvider = LocationManager.NETWORK_PROVIDER;

         // Register the listener with the Location Manager to receive location updates
         locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

         Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

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

         }

然后我根据这些信息获取我的位置:

Geocoder myLocation = new Geocoder(Time.this, Locale.getDefault());
                List<Address> myList=null;
                try {
                    myList = myLocation.getFromLocation(latitude,longitude, 1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            if(myList != null && myList.size()>0) {
                    address= (Address) myList.get(0);


                if(address.getAddressLine(0)!=null){
                    addressStr += address.getAddressLine(0);

                }

                if(address.getAddressLine(1)!=null){
                    addressStr += ", "+address.getAddressLine(1);   

                }
                if(address.getAddressLine(2)!=null){
                    addressStr += ", " +address.getAddressLine(2);  

                }

        }

但有时位置保持为空,直到我重启手机为什么会发生这种情况?有没有办法解决它?

2 个答案:

答案 0 :(得分:2)

尝试设置Location Listener,如下所示:

public class BasicMapActivity_new2 extends Activity implements
LocationListener {

private LocationManager locationManager;
private String provider;
Double Latitude, longitude;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
        .isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
    Toast.makeText(BasicMapActivity_new2.this, "GPS signal not found",
            Toast.LENGTH_LONG).show();
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
} else if (!enabledWiFi) {
    Toast.makeText(BasicMapActivity_new2.this,
            "Network signal not found", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

if (location != null) {

    onLocationChanged(location);
} else {

    // do something
}
}

@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
super.onResume();
  locationManager.requestLocationUpdates(provider, 400, 1, this);
}

Location old_one;

@Override
public void onLocationChanged(Location location) {

double lat = location.getLatitude();
double lng = location.getLongitude();
// Toast.makeText(BasicMapActivity_new.this, "Location " + lat+","+lng,
// Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Latitude = lat;
longitude = lng;

Toast.makeText(BasicMapActivity_new2.this,
        "Location " + coordinate.latitude + "," + coordinate.longitude,
        Toast.LENGTH_LONG).show();

}


 @Override
 public void onProviderDisabled(String provider) {
 // TODO Auto-generated method stub

 }

@Override
 public void onProviderEnabled(String provider) {
 // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

 }

}

不要忘记将permission添加到manifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

更新:这是因为要将requestLocationUpdates() onResume()removeUpdates(this);添加到onPause()。这样,您的应用会在未激活时停止更新的位置。将以下内容添加到您的Activity:

 @Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}

答案 1 :(得分:0)

这是谷歌论坛中的文档问题。检查这个帖子:

https://code.google.com/p/android/issues/detail?id=57707

此外,我认为解决方案是使用Google Location API,这要求您拥有最新的Google Play服务和我认为的&gt; = 2.2。希望这对你有所帮助。我长期与这个问题作斗争

相关问题