- Home /
Android onLocationChange callback not? triggering
I have created a Unity plugin to utilize Android's location services. For all intents and purposes it should be working. I subscribe listeners to location updates, but the LocationListeners' onChangeLocation callback never gets triggered. Recently, I noticed this print out from unity in logcat: Unknown event structure (2). I believe that this is the LocationManager attempting to notify the LocationListener of a location change, but obviously there is some issue. I've searched the net for an answer to this problem, but as the log file gives me little to no information, I was unable to find much information. I've added permissions to the manifest file. Below is the initialization and registration code for the listeners.
Initialization:
public static void Init(Context context)
{
if(_looper == null)
{
Looper.prepare();
_looper = Looper.myLooper();
}
Log.d("LocationServices", "Initializing Location Services");
try
{
_locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
_netProviderIsEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
_gpsProviderIsEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("LocationServices", "NetworkProviderEnabled: " + _netProviderIsEnabled);
Log.d("LocationServices", "GPSProviderEnabled:" + _gpsProviderIsEnabled);
_netLocationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
Log.d("LocationServices", "got a new location");
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d("LocationServices", "NetStatus: " + status);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put("Provider", provider);
statusMap.put("Status", "" + status);
JSONObject json = new JSONObject(statusMap);
//UnityPlayer.UnitySendMessage("RunView", "GetProviderStatus", json.toString());
}
public void onProviderEnabled(String provider)
{
_netProviderIsEnabled = true;
_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _netLocationListener);
}
public void onProviderDisabled(String provider)
{
_netProviderIsEnabled = false;
_locationManager.removeUpdates(_netLocationListener);
}
};
_gpsLocationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
Log.d("LocationServices", "got a new location");
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d("LocationServices", "GPSStatus: " + status);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put("Provider", provider);
statusMap.put("Status", "" + status);
JSONObject json = new JSONObject(statusMap);
//UnityPlayer.UnitySendMessage("RunView", "GetProviderStatus", json.toString());
}
public void onProviderEnabled(String provider)
{
_gpsProviderIsEnabled = true;
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _gpsLocationListener);
}
public void onProviderDisabled(String provider)
{
_gpsProviderIsEnabled = false;
_locationManager.removeUpdates(_gpsLocationListener);
}
};
}
catch(Exception ex)
{
Log.d("LocationServices", "Error: " + ex.getMessage());
}
_isTracking = false;
}
Registration:
public static void StartTracking()
{
if(_isTracking)
return;
Log.d("LocationServices", "Starting Location Services");
try
{
if(_netProviderIsEnabled)
_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _netLocationListener);
/*else
UnityPlayer.UnitySendMessage("RunView", "GetNetProviderStatus", "False");*/
if(_gpsProviderIsEnabled)
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _gpsLocationListener);
/*else
UnityPlayer.UnitySendMessage("RunView", "GetGpsProviderStatus", "False");*/
_currentLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String json = LocationToJson(_currentLocation);
UnityPlayer.UnitySendMessage("RunView", "GetLocationData", json);
}
catch(Exception ex)
{
Log.d("LocationServices", "Error: " + ex.getMessage());
}
_isTracking = true;
}
If you have any idea what could be causing this, any help would be appreciated. If you have any questions let me know and I'll update the post. Thanks all for you help!
Answer by getmetotheforums · Jan 28, 2015 at 04:48 PM
I was not able to resolve this issue, but was able to get past it by using GooglePlayServices Location API: https://developer.android.com/google/play-services/location.html
If you want to have the gps run in the background be sure to use the overloaded version of requestLocationUpdates that takes a PendingIntent as a parameter and be sure to create that PendingIntent with PendingIntent.getService().