Inside
public class IAmHere extends Activity implements LocationListener {
i have
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// 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
}
and
inside
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iamhere);
i have
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
gpsString = (TextView)findViewById(R.id.gpsString);
String Data = "";
String koordinata1 = Double.toString(gps[0]);
String koordinata2 = Double.toString(gps[1]);
Data = Data + koordinata1 + " | " + koordinata2 + "\n";
gpsString.setText(String.valueOf(Data));
but seems it’s not working? Why? I mean even emulator doesn’t want to send GPS data – When I click “send” via UI or console, nothing happens…?
Thank you.
First, you implemented
LocationListeneronIAmHere, then did nothing with that interface.Second, you are calling
getLastKnownLocation(), but you are doing nothing to trigger Android to start collection location data on any provider.These two problems are related.
Location services are off until something registers to get location data from them. Typically, this is via
requestLocationUpdates()onLocationManager…which uses theLocationListenerinterface you implemented.The recipe for using
LocationManageris:LocationListenerviarequestLocationUpdates()LocationListenersometime (e.g.,onDestroy()), so that you do not leak memoryonLocationChanged(), do something usefulHere is a sample project that uses
LocationManagerandLocationListenerin this fashion.