Gps provider is returning Null location
<p>I am trying to get the current location (latitude and longitude values) in my application.
I used the below code.</p>

<p>Problem is-----------------></p>

<pre><code> When the Best provider is GPS,Location returned  is null.
</code></pre>

<p>I learned  some SO posts and I added LocationListener to my code,then it worked but when I restart my device and I run the application again,it gives the same NULL values when Provider is GPS. I don't understand why its not working now.</p>

<p>I also learned that Gps provider is slow in getting values,so I have to add LocationListener to solve this issue.So,I added this to my code</p>

<pre><code>  LocationListener locationListener = new MyLocationListener();
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);
</code></pre>

<p>complete code here  -----------></p>

<pre><code>      public void getCurrentlocation()
    {
        String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Log.e("provider"," "+providers);
        if(!providers.contains("gps")){
          final Intent poke = new Intent();
          poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
          poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
          poke.setData(Uri.parse("3")); 
          sendBroadcast(poke);
          Log.e("your gps is","enabled");
        }
        LocationListener locationListener = new MyLocationListener();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, locationListener);
           // Define the criteria how to select the locatioin provider -&gt; use
           // default
           Criteria criteria = new Criteria();
           String provider = locationManager.getBestProvider(criteria,true);
           Log.e("location provider",""+provider);
           Location location = locationManager.getLastKnownLocation(provider);

           // Initialize the location fields
           if (location != null) 
           {
            System.out.println("Provider " + provider + " has been selected.");
            strLatitude=String.valueOf(location.getLatitude());
                  strLongitude=String.valueOf(location.getLongitude());
                  Log.e("lattitude",strLatitude);
                  Log.e("logntitude",strLongitude);
                  latituteField.setText( strLatitude);
                  longitudeField.setText(strLongitude);
           }
           else 
           {


                  strLatitude=String.valueOf(0);
                  strLongitude=String.valueOf(0);
                  Log.e("lattitude",strLatitude);
                  Log.e("logntitude",strLongitude);
                  latituteField.setText( strLatitude);
                  longitudeField.setText(strLongitude);
           }




     } 



    private class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location)
        {

//            mLattitude = location.getLatitude();
//            mLongitude = location.getLongitude();
//
//            Log.w("value : ",
//                    String.valueOf(location.getLatitude()) + String.valueOf(location.getLongitude())
//                            + 
//
//
//            context.sendBroadcast(new Intent(Constants.BRDCAST_LOCATION_UPDATED));

        }

        @Override
        public void onProviderDisabled(String provider)
        {
            // Log.w("provider", provider);

        }

        @Override
        public void onProviderEnabled(String provider)
        {
            // Log.w("provider", provider);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            // Log.w("status", String.valueOf(status));

        }


}
</code></pre>

<p>please help</p>

<p>Thanks in advance</p>
