Ali
Ali

Reputation: 1678

Mock Location data in android

I just want to receive the location data in my Activity which I am sending from Emulator Control. Following is the code i am using in my main activity:

public class LocationAlert extends Activity {

TextView txtMessage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txtMessage = (TextView)findViewById(R.id.txtMsg);

    LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, mLocListener);


    if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null){
        txtMessage.setText(String.valueOf(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()));
    }

}

LocationListener mLocListener = new LocationListener() {

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

    }

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

    }

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

    }

    @Override
    public void onLocationChanged(Location location) {

        if (location != null){
            txtMessage.setText("Latitude: " + String.valueOf(location.getLatitude()) + ", Longitude: " + String.valueOf(location.getLongitude()));
        }

    }
};

}

but i don't get any data and all the processes running in the emulator closes one by one which i can see in the Devices tab. What's wrong with the above code?

Upvotes: 0

Views: 294

Answers (1)

Sujit
Sujit

Reputation: 10622

You may be missing some android permission in manifest file . add this to your manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Upvotes: 1

Related Questions