我创建了一个Unity插件来利用Android的位置服务。对于所有意图和目的,它应该是有效的。我订阅了位置更新的监听器,但是LocationListeners的onChangeLocation回调永远不会被触发。最近,我注意到这个打印出来的是logcat中的统一:未知的事件结构(2)。我相信这是LocationManager尝试通知LocationListener位置更改,但显然存在一些问题。我在网上搜索了这个问题的答案,但由于日志文件几乎没有给我信息,我无法找到太多信息。我已经为清单文件添加了权限。下面是监听器的初始化和注册代码。
初始化:
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;
}
注册:
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;
}
如果你知道可能导致这种情况的原因,我们将不胜感激。如果您有任何问题,请告诉我,我会更新帖子。谢谢大家的帮助!