LocationManager locationManager= (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
System.out.println("gpsIsEnabled " +gpsIsEnabled +" \n networkIsEnabled "+networkIsEnabled);
if(networkIsEnabled)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else if(gpsIsEnabled)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
System.out.print(location.getLatitude());
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
当我在手机中启用网络时,我总是因为网络启用而变得虚假,我也尝试使用wifi,它总是假的。我用Galaxy S5测试过,总是假的。
答案 0 :(得分:-1)
必须打开位置设置才能使其正常工作。