I have the following very simple class that searches for the last known user coordinates and then prints one of them. But I get loc as a null variable. The application does not read data into the loc variable.
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class Navigation extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getBaseContext(),
"Provider Enable Status : " + mlocManager.isProviderEnabled(
mlocManager.getProvider("gps").getName()),
Toast.LENGTH_LONG).show();
Location loc = mlocManager.getLastKnownLocation("gps");
Toast.makeText( getApplicationContext(), "" +
loc.getLatitude(), Toast.LENGTH_LONG).show();
Toast.makeText( getApplicationContext(), "" + loc,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage() +
e.toString(), Toast.LENGTH_LONG).show();
}
}
}
I added the following listener to the code:
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( mlocManager.getProvider("gps").getName(), 0, 0, mlocListener);
in the oncreate function. And the following as a class in
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Toast.makeText( getApplicationContext(),
"Hi, location changed. :)", Toast.LENGTH_LONG).show();
try {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
" Latitude = " + loc.getLatitude() +
" Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),
Text, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
And after starting the window, I do the following: go to DDMS in Eclipse and set the altitude / longitude values and click on submit.
However, it does not run the toast command in the function.