protected JSONObject doWork() {
Location location;
Double latitude = 0.0;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
}
else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.i("Provider", provider + " has been selected.");
latitude = location.getLatitude();
onLocationChanged(location);
}
}
// latitude = location.getLatitude();
// longitude = location.getLongitude();
}
}
JSONObject result = new JSONObject();
try {
String msg = "KAsun " + mHelloTo + " - its currently "
+ latitude.toString();
result.put("Message", msg);
Log.d(TAG, msg);
} catch (JSONException e) {
}
return result;
我尝试上面的代码通过NETWORK生成当前的GPS位置。但是当我尝试我得到以下错误。我习惯这个代码作为后台服务。你能告诉我为什么会出现这个错误
12-27 09:34:39.406: I/BackgroundService(14495): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()