title: android current location GPS but return null
body: <p>i develop an app to retrieve current  location via GPS but it return null  i want my app use gps only not 3g or wifi   i used  bestprovider but it retrieve only when i run it and not always work</p>
  
  <p>this is the java code :</p>
  
  <pre><code>    package com.example.loc2;
  
      import android.location.Location;
   import android.location.LocationListener;
     import android.location.LocationManager;
    import android.os.Bundle;
     import android.app.Activity;
     import android.content.Context;
     import android.view.Menu;
      import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
     import android.widget.Toast;
  
     public class MainActivity extends Activity {
  private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters  
  private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
  protected LocationManager locationManager;
  protected Button retrieveLocationButton;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
  
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
      locationManager.requestLocationUpdates(
              LocationManager.GPS_PROVIDER, 
              MINIMUM_TIME_BETWEEN_UPDATES, 
              MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
              new MyLocationListener()
      );
  
  retrieveLocationButton.setOnClickListener(new OnClickListener() {
         // @Override
          public void onClick(View v) {
              showCurrentLocation();
          }
  });        
  
  }    
  
  protected void showCurrentLocation() {
  
      Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
      if (location != null) {
          String message = String.format(
                  "Current Location \n Longitude: "+location.getLongitude()+" \n Latitude:"+
                   location.getLatitude());
  
          Toast.makeText(  MainActivity.this,message,
                  Toast.LENGTH_LONG).show();
      }
  
  }   
  
  private class MyLocationListener implements LocationListener {
  
      public void onLocationChanged(Location location) {
          String message = String.format(
                  "New Location \n Longitude: %1$s \n Latitude: %2$s",
                  location.getLongitude(), location.getLatitude()
          );
          Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
      }
  
      public void onStatusChanged(String s, int i, Bundle b) {
          Toast.makeText(MainActivity.this, "Provider status changed",
                  Toast.LENGTH_LONG).show();
      }
  
      public void onProviderDisabled(String s) {
          Toast.makeText(MainActivity.this,
                  "Provider disabled by the user. GPS turned off",
                  Toast.LENGTH_LONG).show();
      }
  
      public void onProviderEnabled(String s) {
          Toast.makeText(MainActivity.this,
                  "Provider enabled by the user. GPS turned on",
                  Toast.LENGTH_LONG).show();
      }
  
  }
  
  }
  </code></pre>
  
  <p>and this is the manifest:
  i used this permission  </p>
  
  <pre><code>  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.loc2"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;/application&gt;
  
      &lt;/manifest&gt;
  </code></pre>
  
  <p>please help </p>