0
當移動數據處於打開狀態時,我無法收到任何位置信息。我的代碼工作正常時,無線網絡連接上,但移動數據未在android的移動數據中獲取位置
不給輸出我的代碼:
private void locationInit() {
// TODO Auto-generated method stub
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
statusOfGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (statusOfGPS) {
speak = "You are in Home Page";
tts = new TextToSpeech(this, this);
Toast.makeText(this,"in location init", Toast.LENGTH_SHORT).show();
} else {
temp = 1;
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
}
gps = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER,
"GPS Not Supported");
network = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER,
"Network Not Supported");
if (gps != null && network != null) {
location = getBetterLocation(gps, network);
} else if (gps != null) {
location = gps;
} else if (network != null) {
location = network;
}
if (location != null) {
onLocationChanged(location);
} else {
latitude = 0.0;
longitude = 0.0;
address = "Unable to get address!";
}
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
try {
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
try {
address = findAddress(latitude, longitude);
tvAddress.setText(address);
} catch (Exception e) {
address = "Unable to get address";
}
} catch (Exception e) {
Log.v("TEST", e.getMessage());
}
}
private String findAddress(double l1, double l2) {
// TODO Auto-generated method stub
String addr, address;
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(l1, l2, 1);
} catch (IOException e1) {
addresses = null;
}
addr = "Unable to get address";
if (addresses != null) {
address = addresses.get(0).getAddressLine(0);
addrs = address;
city = addresses.get(0).getAddressLine(1);
country = addresses.get(0).getAddressLine(2);
if (address == null)
address = "";
if (city == null)
city = "";
addr = address + " " + city + " " + country;
}
return addr;
}
private Location requestUpdatesFromProvider(final String provider,
final String string) {
Location location = null;
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 1000 * 10, 10,
this);
location = locationManager.getLastKnownLocation(provider);
} else {
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
return location;
}
protected Location getBetterLocation(Location newLocation,
Location currentBestLocation) {
if (currentBestLocation == null) {
return newLocation;
}
long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > 1000 * 120;
boolean isSignificantlyOlder = timeDelta < -1000 * 120;
boolean isNewer = timeDelta > 0;
if (isSignificantlyNewer) {
return newLocation;
} else if (isSignificantlyOlder) {
return currentBestLocation;
}
int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
currentBestLocation.getProvider());
if (isMoreAccurate) {
return newLocation;
} else if (isNewer && !isLessAccurate) {
return newLocation;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return newLocation;
}
return currentBestLocation;
}
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
我打電話的onCreate和的onResume的locationInit()函數。 我在清單中擁有以下權限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />