我有一个位置应用,可将手机的位置发送到数据库。当我尝试通过网络获取位置(没有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;
}
}
答案 0 :(得分:0)
1 - 您必须确保您在清单文件中添加了权限:
shim
根据SDK,<2> GPS PROVIDER是:“此提供商使用卫星确定位置。根据条件,此提供商可能需要一段时间才能返回位置修复。”这意味着您必须打开GPS而不是3G