Md Afsar
Md Afsar

Reputation: 5

Message not showing on emulator screen

Here is the code to find the current location, but nothing shows on the screen after pressing the retreive button.
I have reset adb and tried to send the data from emulator control manually. But when I send data using emulator control, then the emulator restarts but never shows the home screen.
Please help. Thanks in advance.

public class LbsGeocodingActivity 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;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.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() {

            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: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(LbsGeocodingActivity.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(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }
}

Upvotes: 0

Views: 576

Answers (2)

Thanos Karpouzis
Thanos Karpouzis

Reputation: 315

On our first attempts to use the location sensors we had similar problems. Two things help the most in order to get the best results from GPS.

On the emulator the Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); causes a lot force closes.

And most important. Its far more stable to input the gps data through telnet. telnet localhost 5554 (You can see the port on the emulators window title). The command for a new gps fix is: geo fix lat lng

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

In you code you might not be receiving the current location(this is because in you need to push your location manually through emulator control).

Just have else case also in showCurrentLocation() and check whether the location object is null or not like below.

protected void showCurrentLocation() {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );

    Toast.makeText(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }else{
       Toast.makeText(LbsGeocodingActivity.this, "location is null",
                Toast.LENGTH_LONG).show();
    }
}  

Upvotes: 1

Related Questions