在没有GPS的情况下获取位置

时间:2018-09-16 14:16:30

标签: java android android-gps

我正在尝试通过网络获取用户位置

我尝试了GPSTracker.java,但是没有用,并且

locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

如果GPS关闭,则返回false

并尝试

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
     Toast.makeText(context,location.getLatitude()+ "", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

我也尝试过

 location = locationManager
                                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

有了它,也没有用;

这些是清单权限

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

谢谢

0 个答案:

没有答案