OMG OMG - 12 days ago 7
Android Question

Android LocationListener does not work

I wrote this code but the

MyLocationListener
class does not work. When I text message like "Find me" the code must give me the approximate location but it does nothing.What is wrong here?

public class SMSReceiver extends BroadcastReceiver {

private LocationManager lm;
private LocationListener locationlistener;
private String senderTel;

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

// get the sms
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";

if (bundle != null) {
senderTel = "";

Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {

msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (i == 0) {
senderTel = msgs[i].getOriginatingAddress();
}

str += msgs[i].getMessageBody().toString();

}

if (str.startsWith("Find me")) {
// ---use the LocationManager class to obtain locations data---
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);

// ---request location updates---
locationlistener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
60000, 1000, locationlistener);
// ---abort the broadcast; SMS messages won’t be broadcasted---



this.abortBroadcast();
}
}

}

private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if (loc != null) {
// ---send a SMS containing the current location---

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(senderTel, null,
"http://maps.google.com/maps?q=" + loc.getLatitude()
+ "," + loc.getLongitude(), null, null);

// ---stop listening for location changes---
lm.removeUpdates(locationlistener);
}
}

@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

}

}

}

Answer

Most probably it does not behave as you expect because you are not testing it the right way. As i understand you expect to get the location right away from the LocationProvider, however that is not how it works.

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
         60000, 1000, locationlistener);

Above you are asking the LocationProvider to update the listener if both conditions are met: time elapsed between the current and previous update is >= 1 minute AND the traveled distance >= 1 km. so the LocationProvider will update the listener according to these conditions, however as the documentation says:

The location update interval can be controlled using the minTime parameter. 
The elapsed time between location updates will never be less than minTime, 
although it can be more depending on the Location Provider implementation 
and the update interval requested by other applications. 

and:

The minDistance parameter can also be used to control the frequency of location 
updates. If it is greater than 0 then the location provider will only send your 
application an update when the location has changed by at least minDistance meters

Moreover you will not receive the first update right away as you expect. From the documentation again:

It may take a while to receive the first location update.

There is a method in the Application class getLastKnownLocation() it provides the last location provided by available tools (GPS/Wifi/GSM). I believe thats what you are looking for.