后台服务中的Android Geo位置始终报告相同的lat lng?

时间:2014-01-16 20:13:05

标签: android geolocation intentservice

我想知道是否有人可以提供帮助,因为我完全糊涂了...我附上了我的所有代码。我已经设置了一个后台服务,它将它的位置报告给XMPP服务器,以记录我们设备的当前纬度/经度。一切都很可爱,我今晚带着我们的设备回家,映射我的旅程。然而,当RDP进入办公室时,我发现每分钟报告给HQ的lat / lng实际上与设备坐在我桌面上的时间相同。

我已经在设备上禁用Wifi,认为网络提供商和wifi可能存在冲突,但我看不到任何变化。

如果有人可以提供帮助,我会非常感激。因为我看不出我做错了什么。我查看了谷歌和各种地理位置示例,据我所知,我的代码应该没问题。

非常感谢。

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
}


}

我的背景服务Geo发布到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();   
}
}

我最初是如何从主要活动

启动服务(每隔1分钟)
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);        
}

权限

<!-- 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" />

1 个答案:

答案 0 :(得分:1)

看起来你实际上没有更新任何位置,所以当你拨打getLastKnownLocation()时,它会告诉你最后一个位置,它知道你的桌子是哪一个。确保您拥有ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限,并确保在设备设置中启用了GPS。