我正在寻找一个非常示例,显示带有 asynctask 的经度和纬度跟踪器。
我发现此代码https://stackoverflow.com/a/6788457但他不起作用:/我在以下位置捕获了这个奇怪的异常“提供程序不存在:null”:
public boolean startService() {
try {
// this.locatorService= new
// Intent(FastMainActivity.this,LocatorService.class);
// startService(this.locatorService);
FetchCordinates fetchCordinates = new FetchCordinates();
fetchCordinates.execute();
return true;
} catch (Exception error) {
Log.i("exception", error.getMessage());
return false;
}
}
搜索后,我看到这篇文章(https://stackoverflow.com/a/13851305/2137454)解释我必须使用这条线:
List<String> providers = locationManager.getAllProviders();
但我不明白在我的 gpstracker 课程中在哪里以及如何使用这些技巧......有人可以帮助我吗?
这里是完整的 gpstracker 类:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location = null;
double latitude = 0;
double longitude = 0;
double altitude = 0;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000* 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context.getApplicationContext();
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.i("Je tombe...","...ici !");
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
Log.i("STOPUSINGGPS", "EFFECTIF");
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
Log.i("LATITUDE", "EFFECTIF");
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
Log.i("LONGITUDE", "EFFECTIF");
longitude = location.getLongitude();
}
return longitude;
}
public double getAltitude() {
if (location != null) {
Log.i("ALTITUDE", "EFFECTIF");
altitude = location.getAltitude();
}
return altitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
编辑:我的 GPS 已打开