我正在使用Trigger IO框架处理HTML5应用程序。我在iPhone上测试它并且它在那里工作得很好。但问题在于Android。调试应用程序时出现以下错误。
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.GpsProvider);
LocationManager.RequestLocationUpdates(LocationManager.GpsProvider, 0, 0, this);
if (CurrentLocation == null)//network provider
{
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.NetworkProvider);
LocationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 0, 0, this);
}
if (CurrentLocation == null)//passive provider
{
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.PassiveProvider);
LocationManager.RequestLocationUpdates(LocationManager.PassiveProvider, 0, 0, this);
}
这适用于从html标头引用的每个资源文件。我的标题代码是:
public class LocationService : Service, ILocationListener
{
public IBinder Binder { get; private set; }
public MainActivity MainAC { get { return m_ac; } set { m_ac = value; } }
public Android.Locations.Location CurrentLocation { get; set; }
public Android.Locations.LocationManager LocationManager { get; set; }
public DateTime LastUpdateTime { get; set; }
public bool IsMockLocation { get; set; }
public string CurrentAddress { get; set; }
public string CurrentCity { get; set; }
private int iUpdateLocationInterval = 30000;// 30sec
private int iUpdateLocationDistance = 100;// 100meter
private int iUpdateLocationInterval_LastKnown = 0;// 0sec
private int iUpdateLocationDistance_LastKnown = 0;// 0meter
private System.Timers.Timer timerBackground = null;
private MainActivity m_ac;
private Lib.GoogleMaps google = new Lib.GoogleMaps();
private bool bUpdateLocationIntervalUnknown = false;
public LocationService()
{
}
#region Override Function
public override void OnCreate()
{
base.OnCreate();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
StartCommandResult result = base.OnStartCommand(intent, flags, startId);
return result;
}
public override void OnDestroy()
{
Binder = null;
if (LocationManager != null)
{
LocationManager.RemoveUpdates(this);
}
base.OnDestroy();
}
public override IBinder OnBind(Intent intent)
{
// Return the communication channel to the service.
this.Binder = new LocalLocationBinder(this);
return this.Binder;
}
#endregion
private void StartBackgroundTimer()
{
timerBackground = new System.Timers.Timer();
timerBackground.Elapsed -= TimerBackground_Elapsed;
timerBackground.Elapsed += TimerBackground_Elapsed;
timerBackground.Interval = 10000;
timerBackground.AutoReset = false;
timerBackground.Enabled = true;
}
private void TimerBackground_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timerBackground.Enabled = false;
try
{
if (CurrentLocation == null)// OnLocationChanged didnt trigger, so get from last known location
{
GetLastKnownLocation();
}
}
catch { }
}
public void GetLastKnownLocation()
{
m_ac.RunOnUiThread(() =>
{
bUpdateLocationIntervalUnknown = true;
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.GpsProvider);
LocationManager.RequestLocationUpdates(LocationManager.GpsProvider, iUpdateLocationInterval_LastKnown, iUpdateLocationDistance_LastKnown, this);
if (CurrentLocation == null)//network provider
{
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.NetworkProvider);
LocationManager.RequestLocationUpdates(LocationManager.NetworkProvider, iUpdateLocationInterval_LastKnown, iUpdateLocationDistance_LastKnown, this);
}
if (CurrentLocation == null)//passive provider
{
CurrentLocation = LocationManager.GetLastKnownLocation(LocationManager.PassiveProvider);
LocationManager.RequestLocationUpdates(LocationManager.PassiveProvider, iUpdateLocationInterval_LastKnown, iUpdateLocationDistance_LastKnown, this);
}
if (CurrentLocation != null)
{
UpdateCurrentLocationInterval();
ResolveGPSCoordinates(CurrentLocation);
}
else
{
m_ac.UpdateLocationName(Function.GetLanguage(m_ac, Resource.String.lblLocationServiceGetFailure));
}
});
}
// Location GPS
public void InitializeLocationManager()
{
try
{
m_ac.RunOnUiThread(() =>
{
try
{
if (LocationManager != null) return;
LocationManager = (LocationManager)GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria
{
Accuracy = Accuracy.Fine
};
IList<string> acceptableLocationProviders = LocationManager.GetProviders(criteriaForLocationService, true);
if (!LocationManager.IsProviderEnabled(LocationManager.GpsProvider))
{
m_ac.ShowMessageToast(Function.GetLanguage(this, Resource.String.lblGPSLocationIsNotEnabled));
return;
}
if (acceptableLocationProviders.Any())
{
StartBackgroundTimer();
LocationManager.RequestLocationUpdates(LocationManager.GpsProvider, iUpdateLocationInterval, iUpdateLocationDistance, this);
LocationManager.RequestLocationUpdates(LocationManager.NetworkProvider, iUpdateLocationInterval, iUpdateLocationDistance, this);
}
else
{
m_ac.ShowMessageToast(Function.GetLanguage(this, Resource.String.lblGPSLocationIsNotEnabled));
}
}
catch(Exception ex) { m_ac.ShowMessageToast("ERROR:" + ex.Message); }
});
}
catch (Exception ex) { m_ac.ShowMessageToast("ERROR:" + ex.Message); }
}
private void UpdateCurrentLocationInterval()
{
try
{
if (LocationManager != null)
{
bUpdateLocationIntervalUnknown = false ;
LocationManager.RequestLocationUpdates(LocationManager.GpsProvider, iUpdateLocationInterval, iUpdateLocationDistance, this);
LocationManager.RequestLocationUpdates(LocationManager.NetworkProvider, iUpdateLocationInterval, iUpdateLocationDistance, this);
}
}
catch { }
}
public void OnLocationChanged(Location location)
{
LastUpdateTime = DateTime.Now;
IsMockLocation = true;
CurrentLocation = location;
CurrentAddress = string.Empty;
CurrentCity = string.Empty;
if (bUpdateLocationIntervalUnknown)
{
UpdateCurrentLocationInterval();
}
if (location.IsFromMockProvider)
{
CurrentLocation = null;
m_ac.UpdateLocationName(CurrentCity);
}
else
{
IsMockLocation = false;
ResolveGPSCoordinates(location);
}
}
private void ResolveGPSCoordinates(Location location)
{
ResolveGPSCoordinatesAwait(location);
}
private async void ResolveGPSCoordinatesAwait(Location location)
{
int iResult = await google.ResolveLatLng(location.Latitude, location.Longitude);
if (iResult == 0)
{
CurrentAddress = google.AddressName;
CurrentCity = google.CityName;
if(CurrentCity == string.Empty)
m_ac.UpdateLocationName(Function.GetLanguage(m_ac, Resource.String.lblLocationServiceGetFailure));
else
m_ac.UpdateLocationName(CurrentCity);
}
else if (iResult == -2)
{
m_ac.UpdateLocationName(Function.GetLanguage(m_ac, Resource.String.lblLocationServiceExceedAPIQuota));
}
else
{
if (string.IsNullOrEmpty(google.APIErrorMessage))
{
m_ac.UpdateLocationName("ERROR:" + location.Latitude + "," + location.Longitude );
}
else
{
m_ac.UpdateLocationName(google.APIErrorMessage);
}
}
}
public void OnProviderDisabled(string provider)
{
if (provider.Equals(LocationManager.GpsProvider, StringComparison.InvariantCultureIgnoreCase))
{
LastUpdateTime = DateTime.Now;
IsMockLocation = false;
CurrentLocation = null;
CurrentAddress = string.Empty;
CurrentCity = Function.GetLanguage(m_ac, Resource.String.lblLocationServiceDisable);
m_ac.UpdateLocationName(CurrentCity);
}
}
public void OnProviderEnabled(string provider)
{
UpdateCurrentLocationInterval();
}
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
{
}
}
public class LocationServiceConnection : Java.Lang.Object, IServiceConnection
{
MainActivity m_ac = null;
public LocationServiceConnection(MainActivity activity)
{
m_ac = activity;
IsConnected = false;
Binder = null;
}
public bool IsConnected { get; private set; }
public LocalLocationBinder Binder { get; private set; }
public void OnServiceConnected(ComponentName name, IBinder service)
{
Binder = service as LocalLocationBinder;
IsConnected = this.Binder != null;
Binder.Service.MainAC = m_ac;
Binder?.Service.InitializeLocationManager();
}
public void OnServiceDisconnected(ComponentName name)
{
IsConnected = false;
Binder.Service.MainAC = null;
Binder = null;
}
public void GetLastKnownLocation()
{
Binder?.Service.GetLastKnownLocation();
}
}
#region LocalBinder
public class LocalLocationBinder : Binder
{
public LocalLocationBinder(LocationService service)
{
this.Service = service;
}
public LocationService Service { get; private set; }
}
#endregion
这两个文件都存在于该位置,并且在iOS上运行良好。但问题在于Android / Chrome。任何解决方法?
修改
以下是index.html的标题:
Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME
注意:如果我删除了jquery.mobile-1.4.5.min.js文件,那么它将引用index.js
答案 0 :(得分:1)