title: Android LocationManager network provider returns null
body: <p>I wanted to get my GPS coordinates using Android App. I started developing, and I can get GPS coordinates, but they are not accurate. I wanted to use NETWORK_PROVIDER, but the Location by this provider is always null. More interesting, isProvicerEnabled returns true.</p>
  
  <p>I used example from this thread (best answer)
  <a href="https://stackoverflow.com/questions/15997079/getlastknownlocation-always-return-null-after-i-re-install-the-apk-file-via-ecli">enter link description here</a></p>
  
  <pre><code>private void _getLocation() {
      // Get the location manager
      try {
          boolean isGPSEnabled = false;
          boolean isNetworkEnabled = false;
          locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
          Location location = null;
          double latitude = -1;
          double longitude = -1;
  
          // getting GPS status
          isGPSEnabled = locationManager
                  .isProviderEnabled(LocationManager.GPS_PROVIDER);
  
          // getting network status
          isNetworkEnabled = locationManager
                  .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
          if (!isGPSEnabled &amp;&amp; !isNetworkEnabled) {
              // no network provider is enabled
          } else {
              if (isNetworkEnabled) {
                  showToast("network");
                  locationManager.requestLocationUpdates(
                          LocationManager.NETWORK_PROVIDER,
                          1000,
                          0, this);
                  Log.d("Network", "Network Enabled");
                  if (locationManager != null) {
                      location = locationManager
                              .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                      if (location != null) {
                          latitude = location.getLatitude();
                          longitude = location.getLongitude();
                      }
                  }
              }
              // if GPS Enabled get lat/long using GPS Services
              else if (isGPSEnabled) {
                  showToast("gps");
                  if (location == null) {
                      locationManager.requestLocationUpdates(
                              LocationManager.GPS_PROVIDER,
                              1000,
                              0, this);
                      Log.d("GPS", "GPS Enabled");
                      if (locationManager != null) {
                          location = locationManager
                                  .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                          if (location != null) {
                              latitude = location.getLatitude();
                              longitude = location.getLongitude();
                          }
                      }
                  }
              }
          }
          showToast("" + latitude + longitude);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>I have all the permissions in manifest</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt; 
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt; 
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  </code></pre>
  
  <p>I know the code is dirty, but for now it's only for testing. Do I miss something? I found similar examples in many places, and it seems pretty straight, so I am a little confused.
  My phone works ok, GPS and network works fine. For example Google Maps application I have works well. Any suggestions?</p>