如果Android手机没有互联网连接,如何将提供商切换为GPS_provider?
我在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
吗?谢谢
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();
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onLocationUpdate()
只是检查互联网是否可用,如果
互联网不可用
那么就像这样,
编辑:在这里你可以找到关于 Hoe 的漂亮解释当前位置,
在 Android 中获取用户当前位置的最简单、最可靠的方法是什么?
另请参阅此内容。
谢谢
onLocationUpdate()
just check for Internet is available or not,If
Internet is not available
then just,so like this,
EDIT: Here you can find beautiful explanation about Hoe to get current location,
What is the simplest and most robust way to get the user's current location in Android?
please refer this also.
Thanks