我有一个后台服务来跟踪用户的位置。在服务中,我有一个带有Integer键和LatLng值的哈希图。该服务将用户的位置与LatLng值进行比较。我的问题是,有时我需要向哈希图添加另一个值。
我已经尝试了2件事。
那么我更新服务中的哈希表的最佳方法是什么?
编辑这是我的服务代码-
public class LocationService extends Service {
private static final String TAG = "LOCATIONTEST";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
public static HashMap<Integer, LatLng> mLocationList;
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
// DO stuff here with update mLocationList HashMap
// This should be the update list
mLocationList = Hawk.get("LocationHashmap");
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
mLocationList =Hawk.get("LocationHashmap", new HashMap<Integer, LatLng>());
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
在另一个片段中,首先通过从本地存储中检索列表,然后更新列表,然后再次保存,来更新列表。
HashMap<Integer, LatLng> locationList = Hawk.get("LocationList");
locationList.put(order.getId(), latLng);
Hawk.put("LocationList", locationList;
现在,当调用OnLocationChanged时,我尝试调用Hawk.get(“ LocationList”)以获取更新的哈希图,但未更新。我认为该库有问题,但是我不确定还有其他事情。我还尝试过将服务中的列表设为静态,然后仅更新该列表。因此,代替上面片段中的代码,它看起来像这样:
LocationService.mLocationList.put(order.getId(), latLng)
但是该列表没有更新。
在OnCreate中,我从本地存储获取HashMap。假设列表大小为2。在应用程序的其他地方,我更新该列表,现在列表大小为3。现在,当服务命中onLocationChanged时,我要遍历哈希图中的所有值。但是,如果我尝试从本地存储中获取相同的哈希值以获取更新哈希图,则大小仍然为2。如何在服务中更新该哈希图?