GPS Android - Cannot retrieve location on actual phone but works on emulator
<p>I cannot the get the latitude and longitude of my phone when testing it on an actual phone or device, it always return <strong>null</strong> values, but when I test it on the emulator and send coordinates using DDMS, it succesfully returns the lat and long.</p>

<p>Here is my class:</p>

<pre><code>package locateodroid.james;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.EditText;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver{

    public SharedPreferences prefs;
    private String Keywords = "prefmode";
    private Double lat;
    private Double lon;
    private static final String FAR_MODE_KEY = "farmode";
    private Context con;

    @Override
    public void onReceive(Context context, Intent intent) {

        con = context;
        String FarModeKeyword= "";
        String message = "";
        String number = "";
        prefs = context.getSharedPreferences(Keywords, Context.MODE_PRIVATE);
        FarModeKeyword = prefs.getString(FAR_MODE_KEY, "");

        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i&lt;msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                message += msgs[i].getMessageBody().toString();
            }

            for (int i=0; i&lt;msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                number += msgs[i].getOriginatingAddress();
            }

        }


            getLocation();
            Toast.makeText(context,
                    lat + "_" + lon,
                    Toast.LENGTH_SHORT).show();
            sendSMS(number, lat + "_" + lon);


    }

    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

    private getLocation ()
    {
        // Get the location manager
        LocationManager locationManager = (LocationManager)con.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria ();
        Geocoder geoCoder = new Geocoder(con);
        LocationListener MyLocListener = new LocListener();
        List&lt;String&gt; providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 0, 0, MyLocListener);
        }

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lat = location.getLatitude();
        lon = location.getLongitude();

    }


    public class LocListener implements LocationListener
    {     
        @Override
          public void onLocationChanged(Location loc) {
              lat = loc.getLatitude();
              lon = loc.getLongitude();       
          }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }   
    }
}
</code></pre>

<p>Thanks in advance! </p>
