【发布时间】:2016-01-16 12:28:40
【问题描述】:
我有一个定位应用,可以将我手机的位置发送到数据库。当我尝试通过网络(不开启 3G)获取位置时,它可以工作!但是当我打开我的 3G 时它不起作用,因为代码: locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
总是返回 NULL。这是我的完整位置处理程序..
我的 locationhandler 类的代码。
package info.androidhive.loginandregistration.location;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class LocationHandler {
Context mContext = null;
Context ctx;
double Lat;
double Long;
Location Localization;
LocationManager locationManager;
boolean GPSActivate = false;
boolean GPRSActivate = false;
boolean getUbicationbool;
private double mLastLatitudeLocation = 0;
private double mLastLongitudeLocation = 0;
Intent intent;
// flag for GPS status
public boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
public LocationHandler(Context context) {
mContext = context;
}
private void ConfigurationManager() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.v("isGPSEnabled", "=" + isGPSEnabled);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.v("isNetworkEnabled", "=" + isNetworkEnabled);
}
public Location getLocation() {
try {
ConfigurationManager();
if (isGPSEnabled == false && isNetworkEnabled == false) {
// no network provider is enabled
return null;
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
location = null;
Log.d("Network", "Network - GET LOCATION ");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
location = null;
if (location == null) {
Log.d("GPS Enabled", "GPS Enabled - GET LOCATION");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
【问题讨论】: