如何切换到供应商如果GPS_provider Android手机没有上网吗? [英] How to switch provider into GPS_provider if android phone doesn't have internet connection?
问题描述
我在Android的位置的应用程序。这里的code获取经纬度和长期价值。
i make a location application on android. here's the code for acquiring lat and long value.
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();
}
这code将工作,如果我有一个互联网连接( NETWORK_PROVIDER
)。有没有人知道如何修改code自动切换到 GPS_PROVIDER
当Android手机没有任何互联网连接?谢谢
that code will works if i have an internet connection (NETWORK_PROVIDER
). has anyone know how to modify that code for automatically switch into GPS_PROVIDER
when android phone doesn't have any internet connection?? thanks
推荐答案
onLocationUpdate()
只是检查网络是否可用,
onLocationUpdate()
just check for Internet is available or not,
如果互联网不可用
然后只是,
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
让喜欢本,
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
return false;
}
编辑:在这里,你可以找到有关锄美丽的解释来获得当前位置,
Here you can find beautiful explanation about Hoe to get current location,
<一个href=\"http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655\">What是让Android中用户的当前位置的最简单,最可靠的方法?
请同时参阅本。
感谢
这篇关于如何切换到供应商如果GPS_provider Android手机没有上网吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!