gpt4 book ai didi

后台服务中的 Android Geo Location 总是报告相同的经纬度?

转载 作者:太空狗 更新时间:2023-10-29 14:16:44 27 4
gpt4 key购买 nike

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

我认为网络提供商和 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();
}
}

以及我最初如何从主 Activity 启动服务(以 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() 时,它会为您提供它知道的最后一个位置,即您的办公 table 。确保您拥有 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION 权限,并确保在设备设置中启用了 GPS。

关于后台服务中的 Android Geo Location 总是报告相同的经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21171895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com