【发布时间】:2011-10-15 14:03:56
【问题描述】:
我在 android 上制作了一个位置应用程序。这是获取纬度和经度值的代码。
public void geoLocation()
{
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
updateWithNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {
Toast.makeText(ListenSMSservice.this,"Network Enabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(ListenSMSservice.this,"Network Disabled",Toast.LENGTH_SHORT ).show();
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
public void updateWithNewLocation(Location location) //update posisi terkini
{
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Geocoder gc = new Geocoder(ListenSMSservice.this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
//latLongString = "Lat:" + lat + "\nLong:" + lng;
latLongUrl="Phone Map\nhttp://maps.google.com/maps?q=" + lat + ",+" + lng + "+(Your+phone+location)&iwloc=A&hl=en\n";
} else {
latLongUrl = "No location found";
}
myMessage=latLongUrl+"\n"+addressString;
locationManager.removeUpdates(locationListener);
locationManager = null;
//Toast.makeText(ListenSMSservice.this, myMessage, 3).show();
sendsms();
}
如果我有互联网连接 (NETWORK_PROVIDER),该代码将有效。有谁知道如何修改该代码以在android手机没有任何互联网连接时自动切换到GPS_PROVIDER?谢谢
【问题讨论】:
标签: java android android-manifest