0

I use the LocationManager to get my current location,but I can't,why? I have already add permission

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

The following is my code:

public class MainActivity extends Activity {

private TextView mLocation;
public LocationManager locationManager;
private String locationProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLocation = (TextView) findViewById(R.id.tv_location);
    getLocation();
}

private void getLocation() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
        // 如果是GPS
        locationProvider = LocationManager.GPS_PROVIDER;
    } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        // 如果是Network
        locationProvider = LocationManager.NETWORK_PROVIDER;
    } else {
        Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
        return;
    }
    // 获取Location
    Location location = locationManager.getLastKnownLocation(locationProvider);
    if (location != null) {
        // 不为空,显示地理位置经纬度
        showLocation(location);
    }else{
        // 监视地理位置变化
        locationManager.requestLocationUpdates(locationProvider, 0, 0,mListener);
    }

}

LocationListener mListener = new LocationListener() {
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    // 如果位置发生变化,重新显示
    @Override
    public void onLocationChanged(Location location) {
        showLocation(location);
    }
};

/** 显示地理位置经度和纬度信息 */
private void showLocation(Location location) {
    String locationStr = "维度:" + location.getLatitude() + "\n" 
            + "经度:"+ location.getLongitude();
    mLocation.setText(locationStr);
}

}

Json zhang
  • 119
  • 14

1 Answers1

0

In Android 6 and 7 accessing the location is considered a 'dangerous permission' that has to be requested at runtime.

https://developer.android.com/training/permissions/requesting.html

You can also grant that permission in your system settings (apps > your app > permissions)

C. L.
  • 571
  • 1
  • 8
  • 26
  • my phone is 5.0.2 – Json zhang Nov 28 '16 at 09:41
  • OK, on Android 5 it should work. What exactly is the issue yor're facing? Do you get an error message? – C. L. Nov 28 '16 at 09:43
  • no error message, I have a textview , I want to get the latitude and longitude and then show at the textview ,but it does't work – Json zhang Nov 28 '16 at 09:46
  • I debug and find the code not go `if (location != null)` it goes to `else`, and into listener,but it didn't run the method `onLocationChanged`,so that ,I need move ? – Json zhang Nov 28 '16 at 09:54
  • Do you get a enabled locationProvider? Which one? Is the locationg service enabled on your device? – C. L. Nov 28 '16 at 09:54
  • yes, I get the locationprovider is `LocationManager.GPS_PROVIDER` – Json zhang Nov 28 '16 at 09:56
  • 1
    It's not unusual that the last known location is null when starting the location service, `onLocationChanged()` should be called as soon your GPS provider gets the location. This may take some time... – C. L. Nov 28 '16 at 09:58