Question

I wonder if anyone can help, because I'm completely confused... I've attached all my code. I've set up a Background service that reports it's location to an XMPP server, to record the current lat/lng of our devices. Everything works lovely and I've brought one of our devices home with me tonight, to map my journey. However, when RDP'ing into the office, I've found out that the lat/lng being reported to HQ every minute is in fact identical to that of when the device was sat on my desk.

I've disabled Wifi on the device thinking that network provider and wifi may be conflicting, but I'm seeing no change.

If anyone can help I'd really appreciate it. Because I can't see what I'm doing wrong. I've checked google and various geolocation examples and as far as I can see, my code should be fine.

Many thanks.

package com.goosesys.gooselib.GeoLocation;


/*
 * GeoLocation Class
 * GooseLib
 * 
 * Used to access coarse and fine geo-location metrics based on the devices' last known location
 * 
 * PERMISSIONS REQUIRED:
 * INTERNET, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION
 */
import com.goosesys.gooselib.Logging;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GeoLocation implements LocationListener 
{
private LocationManager locationManager = null;
private String provider = "";
@SuppressWarnings("unused")
private double latitude = 0;
@SuppressWarnings("unused")
private double longitude = 0;
@SuppressWarnings("unused")
private Context context = null;

public GeoLocation(Context context)
{
    // used to add dialog prompts to the main activity
    this.context = context;
    locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);        
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null)
    {
        Logging.Info("GooseLib", "GeoLocation - provider [" + provider + "] has been selected");
        onLocationChanged(location);
    }
    else
    {
        locationManager = getLocationUpdates();
        Logging.Warning("GooseLib", "GeoLocation - Location not available");
    }
}

/*
 * Returns the location manager as an object after calling for location updates
 */
public LocationManager getLocationUpdates()
{
    locationManager.requestLocationUpdates(provider, 100, 1, this);

    return locationManager;
}

/*
 * return the latitude of the current devices' location - last known location
 */
public double getLatitude()
{
    double lat = 0;
    try
    {
        lat = locationManager.getLastKnownLocation(provider).getLatitude();
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }

    return lat;
}

/*
 * returns the longitude of the current devices' location - last known location
 */
public double getLongitude()
{
    double lng = 0;
    try
    {
        lng = locationManager.getLastKnownLocation(provider).getLongitude();            
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }

    return lng;
}

@Override
public void onLocationChanged(Location location) 
{
    this.latitude = location.getLatitude();
    this.longitude = location.getLongitude();
}

@Override
public void onProviderDisabled(String arg0) 
{
    Logging.Warning("GooseLib", "GeoLocation - Provider Disabled [" + provider + "]");
}

@Override
public void onProviderEnabled(String arg0) 
{
    Logging.Info("GooseLib", "GeoLocation - Provider Enabled [" + provider + "]");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
{       
    // We don't need to worry about this method - just yet
}


}

And my Background Service for Geo posting to XMPP.

package com.goosesys.dta_pta_test;

import org.jivesoftware.smack.packet.Message;

import android.app.IntentService;
import android.content.Intent;
import com.google.gson.Gson;
import com.goosesys.gooselib.GeoLocation.GeoLocation;
import com.goosesys.gooselib.Transactions.Geo;
import com.goosesys.gooselib.Transactions.MessageType;
import com.goosesys.gooselib.Transactions.XMPPTransaction;
import com.goosesys.gooselib.Utilities.AppSettings;
import com.goosesys.gooselib.Utilities.Utility;

public class BGGeoProc extends IntentService 
{

public BGGeoProc()
{
    super("BGGeoProc");
}

public BGGeoProc(String name) 
{
    super("BGGeoProc");
}

public void updateLocation()
{
    GeoLocation gl = new GeoLocation(getApplicationContext());
    double lng = gl.getLongitude();
    double lat = gl.getLatitude();

    MessageType mt = new MessageType(MessageType.Type.GEO);
    Geo g = new Geo(lng, lat);
    XMPPTransaction<Object> trans = new XMPPTransaction<Object>(mt, g);

    Message m = new Message();
    m.setTo(AppSettings.BOT_NAME);
    m.setFrom(Utility.getAndroidID(getApplicationContext()));
    m.setBody(new Gson().toJson(trans));

    Intent s = new Intent(getApplicationContext(), XMPPIntentService.class);
    s.putExtra(XMPPIntentService.MESSAGEDATA, new Gson().toJson(m));
    startService(s);
}

@Override
protected void onHandleIntent(Intent intent)
{           
    // Worker thread area //
    updateLocation();   
}
}

And how I originally start the service (in 1 minute intervals) from the Main Activity

private void startGeoLocationSender()
{
    // do some work here
    Calendar cal = Calendar.getInstance();

    Intent i = new Intent(this, BGGeoProc.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, i, 0);

    Logging.Info(AppSettings.APP_TAG, "Setting Alarm Manager for BGGeoProc");

    // create an alarm manager to hook into system wide calendar
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
            AppSettings.COLLECTOR_PROC_1_MINS, pintent);        
}

Permissions

<!-- PERMISSION ACCESS -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
Was it helpful?

Solution

It looks like you weren't actually getting any location updated so when you call getLastKnownLocation() it's giving you the last location it knows about which is your desk. Make sure that you have ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions and also make sure that the GPS is enabled in the device settings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top