title: finding location from requestLocationUpdates() from GPS not working
body: <p>I am creating a map application, which gets location from GPS, i'm finding location from requestLocationUpdates(), but i'm not getting any response from GPS provider, however when i'am using network provider i'am getting location, can anyone help where i'm wrong, </p>
  
  <p>Also i'am new to android app development, so i'll appreciate any tips to optimize my code(make more efficient with less mess),</p>
  
  <p>Here's my code : </p>
  
  <pre><code>public void trackLocation(){
      if (ContextCompat.checkSelfPermission(activity.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
          locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
          boolean flag;
          Boolean dlg;
          flag = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          if (flag) {
              Log.v("function", "onClick");
              myLocationListener = new MyLocationListener();
              dlg = true;
          } else {
              dlg = gpsDialog();
              progressBar.setVisibility(View.GONE);
          }
  
          Log.d("maps", "Requesting location updates");
          if(dlg) {
              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
          }
      }
  }
  
  public class MyLocationListener implements LocationListener {
  
      @Override
      public void onLocationChanged(Location location) {
          if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              return;
          }
          Log.d("maps", "on location changed : "+location.getLatitude() + "---"+ location.getLongitude());
          locationManager.removeUpdates(myLocationListener);
      }
  
      @Override
      public void onStatusChanged(String s, int i, Bundle bundle) {
          Log.d("maps" , "Status Changed");
      }
  
      @Override
      public void onProviderEnabled(String s) {
          Log.d("maps" , "Provider Enabled");
      }
  
      @Override
      public void onProviderDisabled(String s) {
          Log.d("maps" , "Provider Disabled");
      }
  }
  
  public Boolean gpsDialog(){
      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
      builder.setMessage("Do you want to start GPS?")
              .setCancelable(false)
              .setTitle("GPS DISABLED")
              .setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              // finish the current activity
                              Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                              activity.startActivity(myIntent);
                              dialog.cancel();
                          }
                      })
              .setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              // cancel the dialog box
                              dialog.cancel();
                          }
                      });
      AlertDialog alert = builder.create();
      alert.show();
      progressBar.setVisibility(View.GONE);
      return false;
  }
  </code></pre>