locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 使用 Wi-Fi 总是返回 false
Posted
技术标签:
【中文标题】locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 使用 Wi-Fi 总是返回 false【英文标题】:locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) use Wi-Fi always return false 【发布时间】:2016-03-22 07:26:42 【问题描述】:我试过了
NETWORK_PROVIDER 获取该地点的纬度和经度。
我的测试设备是 LG Optimus G Pro(LG-F240L) android 4.4.2,API 19。
使用 Wi-Fi 网络,不是 4G、3G
“isProviderEnabled(LocationManager.NETWORK_PROVIDER)”总是返回 false。
谁能帮帮我?提前谢谢!
这是我的代码:
public class GetCurrentLocationActivity extends Service implements LocationListener
private final Context mContext;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean isGetLocation = false;
Location location;
double latitude;
double longitude;
public GetCurrentLocationActivity(Context context)
this.mContext = context;
getLocation();
public Location getLocation()
try
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("test",""+isGPSEnabled);
Log.d("test1",""+isNetworkEnabled);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled)
showSettingsAlert();
else
this.isGetLocation = true;
if(isNetworkEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
if(locationManager!=null)
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null)
latitude = location.getLatitude();
longitude = location.getLongitude();
if(isGPSEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
if(locationManager!=null)
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null)
latitude = location.getLatitude();
longitude = location.getLongitude();
else
showSettingsAlert();
catch (Exception e)
e.printStackTrace();
return location;
public void stopUsingGPS()
if(locationManager!=null)
locationManager.removeUpdates(GetCurrentLocationActivity.this);
public double getLatitude()
if(location!=null)
latitude = location.getLatitude();
return latitude;
public double getLongitude()
if(location!=null)
longitude = location.getLongitude();
return longitude;
public boolean isGetLocation()
return this.isGetLocation;
public void showSettingsAlert()
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS 사용유무셋팅");
alertDialog.setMessage("GPS 셋팅이 되지 않았을수도 있습니다.\n 설정창으로 가시겠습니까?");
// OK 를 누르게 되면 설정창으로 이동합니다.
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
);
// Cancle 하면 종료 합니다.
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
dialog.cancel();
);
alertDialog.show();
@Override
public IBinder onBind(Intent arg0)
return null;
public void onLocationChanged(Location location)
// TODO Auto-generated method stub
public void onStatusChanged(String provider, int status, Bundle extras)
// TODO Auto-generated method stub
public void onProviderEnabled(String provider)
// TODO Auto-generated method stub
public void onProviderDisabled(String provider)
// TODO Auto-generated method stub
这是我的许可:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
【问题讨论】:
【参考方案1】:代码看起来没问题。就权限而言,您只需要前两个:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
替换条件以检查您的 GPS 是否已启用,如下所示:
if (isGPSEnabled)
if (location == null)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener)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();
//end-fi isGPSEnabled
【讨论】:
我会尽量去我家。谢谢:D 谢谢!为我工作。突然“isProviderEnabled(LocationManager.NETWORK_PROVIDER)”值为真以上是关于locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) 使用 Wi-Fi 总是返回 false的主要内容,如果未能解决你的问题,请参考以下文章