title: extract current location only works in my phone
body: <p>The code works in my phone properly but doesn't works sometime. Also, this code doesn't work in other phone properly. I have tried many times. And while sending this app's apk file through xender the installation fails. Please help to improve my code so that it works properly in all phone when location is turned ON.</p>
  
  <p>Mainactivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  TextView result;
  Double latitude, longitude;
  Geocoder geocoder;
  List&lt;Address&gt; addressList;
  Getgps gps;
  Context mContext;
  
  protected LocationManager locationManager;
  boolean isGPSEnabled = false;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      result = (TextView) findViewById(R.id.abcd);
  
      geocoder = new Geocoder(this, Locale.getDefault());
      mContext = this;
  
  
      try
      {
          locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
          isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          if (isGPSEnabled)
          {
              getlonglat();   //check location
  
              if(CheckInternetConnection(MainActivity.this)) {
                  getaddress();
                  // getlocation
                  if(result.getText().toString().matches(""))
                      {
                          Toast.makeText(mContext, "Poor Internet Connection", Toast.LENGTH_SHORT).show();
                      }
              }
              else
              {
                  Toast.makeText(getApplicationContext(),"No internet Connection",Toast.LENGTH_LONG).show();
              }
          }
          else
          {
              Toast.makeText(getApplicationContext(),"Turn ON Your Location",Toast.LENGTH_LONG).show();
              showgpsSettingsAlert();
              startActivity(new Intent(MainActivity.this, Main2Activity.class));
  
  
              Toast.makeText(mContext, " after delay khatey", Toast.LENGTH_SHORT).show();
          }
  
      }
  
      catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  
  
  public void getlonglat() {
      if (ContextCompat.checkSelfPermission(mContext,
              Manifest.permission.ACCESS_FINE_LOCATION)
              != PackageManager.PERMISSION_GRANTED
              &amp;&amp; ActivityCompat.checkSelfPermission(mContext,
              Manifest.permission.ACCESS_COARSE_LOCATION)
              != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
      }
      else {
          Toast.makeText(mContext, "GPS Permission Granted", Toast.LENGTH_SHORT).show();
          gps = new Getgps(mContext, MainActivity.this);
          // Check if GPS enabled
          if (gps.canGetLocation()) {
              latitude = gps.getLatitude();
              longitude = gps.getLongitude();
  
              Toast.makeText(getApplicationContext(), "Your Location is - \nLatitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
          }
      }
  }
  
  
  public void getaddress() {
      try {
          addressList = geocoder.getFromLocation(latitude, longitude, 1);
  
          String address = addressList.get(0).getAddressLine(0);
          String area = addressList.get(0).getLocality();
          String city = addressList.get(0).getAdminArea();
          String country = addressList.get(0).getCountryName();
          String postalcode = addressList.get(0).getPostalCode();
          String fullAddress = address + ", " + area + ", " + city + ", " + country + ", " + postalcode;
          Toast.makeText(mContext, "Internet Permission Granted", Toast.LENGTH_SHORT).show();
          result.setText(fullAddress);
  
      }
      catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  
  public static boolean CheckInternetConnection(Context context) {
      ConnectivityManager connectivity =
              (ConnectivityManager) context.getSystemService(
                      Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i &lt; info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                      return true;
                  }
      }
      return false;
  }
  
  public void showgpsSettingsAlert() {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  
      alertDialog.setCancelable(false);
      this.setFinishOnTouchOutside(false);
  
      alertDialog.setTitle("GPS Setting");
      alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  
      alertDialog.setPositiveButton("OK          ", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
              Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              mContext.startActivity(intent);
          }
      });
  
      alertDialog.setNegativeButton("Cancel                                         ", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();
              Toast.makeText(mContext, "Sorry we cannot proceed", Toast.LENGTH_SHORT).show();
          }
      });
  
      alertDialog.show();// Showing Alert Message
  }
  }
  </code></pre>
  
  <p>Getgps.java</p>
  
  <pre><code>package com.example.bibash28.locationfinal2;
  
  /**
   * Created by Bibash28 on 11/3/2017.
   */
  
  public class Getgps extends Service
  {
  
  private Context mContext; // Flag for GPS status
  boolean isGPSEnabled = false; // Flag for network status
  boolean canGetLocation = false;
  Location location;  // Location
  
  Double latitude,longitude;
  
  private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;// The minimum distance to change Updates in meters
  private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;// 10 meters// The minimum time between updates in milliseconds// 1 minute
  
  Activity activity;
  protected LocationManager locationManager;
  
  public Getgps(Context context, Activity activity)
  {
      this.mContext = context;
      this.activity = activity;
      getLocation();
  }
  
  public Location getLocation()
  {
      try
      {
          locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
          // Getting GPS status
          isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          // If GPS enabled, get latitude/longitude using GPS Services
          this.canGetLocation = true;
          if (isGPSEnabled) {
              if (location == null) {
                  if (ContextCompat.checkSelfPermission(activity,
  
                          Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp;
  
                          ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
  
                  } else {
                      locationManager.requestLocationUpdates(
                              LocationManager.GPS_PROVIDER,
                              MIN_TIME_BW_UPDATES,
                              MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener);
                      Log.d("GPS Enabled", "GPS Enabled");
                      if (locationManager != null) {
                          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                          if (location != null) {
                              latitude = location.getLatitude();
                              longitude = location.getLongitude();
                          }
                      }
                  }
              }
  
          }
      }
  
      catch (Exception e) {
          e.printStackTrace();
      }
      return location;
  }
  
  
  
  private final LocationListener mLocationListener = new LocationListener() {
  
      @Override
      public void onLocationChanged(final Location location) {
  
          if (location != null) {
              latitude = location.getLatitude();
              longitude = location.getLongitude();
          }
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
      }
  };
  
  //Function to get latitude
  public double getLatitude() {
      if (location != null) {
          latitude = location.getLatitude();
      }
      return latitude;
  }
  
  
  //  Function to get longitude
  public double getLongitude() {
      if (location != null) {
          longitude = location.getLongitude();
      }
      return longitude;
  }
  
  //Function to check GPS/Wi-Fi enabled   @return boolean
  
  public boolean canGetLocation()
  {
      return this.canGetLocation;
  }
  
  
  @Override
  public IBinder onBind(Intent arg0) {
      return null;
  }
  }
  </code></pre>