我不知道是否有人能帮忙,因为我完全糊涂了…我把我所有的密码都贴上了。我已经建立了一个后台服务,向xmpp服务器报告它的位置,以记录我们设备的当前lat/lng。一切都很好,我今晚带了我们的一个设备回家,来绘制我的旅程。然而,当我进入办公室时,我发现每分钟向总部报告的lat/lng事实上与设备放在我桌上的时候是一样的。
我已经禁用了设备上的WiFi,认为网络提供商和WiFi可能有冲突,但我没有看到任何变化。
如果有人能帮忙,我会非常感激的。因为我看不出我做错了什么。我已经检查过google和各种地理位置示例,据我所见,我的代码应该没问题。
多谢。

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
}


}

以及我的背景服务,用于向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" />

最佳答案

看起来你实际上没有得到任何位置的更新,所以当你打电话给getLastKnownLocation()时,它会给你最后一个它知道的位置,那就是你的办公桌。确保您具有ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限,并确保在设备设置中启用GPS。

07-24 09:49
查看更多