Java 如果android手机没有';你不能上网吗?
我在android上做了一个定位应用程序。以下是获取lat和long值的代码Java 如果android手机没有';你不能上网吗?,java,android,android-manifest,Java,Android,Android Manifest,我在android上做了一个定位应用程序。以下是获取lat和long值的代码 public void geoLocation() { locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() { public void onLocationChange
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();
}
公共空间地理定位()
{
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
已更改位置上的公共无效(位置){
//当网络位置提供程序找到新位置时调用。
updateWithNewLocation(位置);
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
公共无效onProviderEnabled(字符串提供程序){
Toast.makeText(listensmservice.this,“已启用网络”,Toast.LENGTH_SHORT.show();
}
公共无效onProviderDisabled(字符串提供程序){
Toast.makeText(listensmservice.this,“网络已禁用”,Toast.LENGTH_SHORT.show();
}
};
//向位置管理器注册侦听器以接收位置更新
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener);
}
public void updateWithNewLocation(位置位置)//更新posisi terkini
{
如果(位置!=null){
lat=位置。getLatitude();
lng=location.getLongitude();
Geocoder gc=新的Geocoder(listensmservice.this,Locale.getDefault());
试一试{
列表地址=gc.getFromLocation(lat,lng,1);
StringBuilder sb=新的StringBuilder();
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i
如果我有internet连接(NETWORK\u PROVIDER
),则该代码将起作用。有人知道当android手机没有任何互联网连接时,如何修改自动切换到GPS\U提供商的代码吗??谢谢onLocationUpdate()
只需检查互联网是否可用
如果互联网不可用
则
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;
}
编辑:在这里你可以找到关于锄头的美丽解释,以获得当前位置
请同时参阅此
谢谢