シンプルな追跡アプリをまとめようとしています。15 秒ごとにデバイスの位置を webapi (Web サービス) サービスに送信するのではなく、位置の更新を登録するだけです。以下は、サーバーへの登録と投稿を行う次のコードです。
#region START STOP TRACKING
public void StartTracking()
{
if (currentAsset == null) { return; }
isTracking = true;
//SETUP THE LOCATION VARIALBES
if (_geocoder == null) { _geocoder = new Geocoder(this); }
if (_locationManager == null) { _locationManager = (LocationManager)GetSystemService(LocationService); }
var criteria = new Criteria() { Accuracy = Accuracy.Fine };
string bestProvider = _locationManager.GetBestProvider(criteria, true);
Location lastKnownLocation = _locationManager.GetLastKnownLocation(bestProvider);
//LOG RECORD FOR CURRENT LOCATION
if (lastKnownLocation != null)
{
PostData(lastKnownLocation, DateTime.Now);
}
_locationManager.RequestLocationUpdates(bestProvider, 2000, 1, this);
BusTimer.Start();
}
private void OnTimeEvent(object source, ElapsedEventArgs e)
{
RunOnUiThread(delegate
{
var criteria = new Criteria() { Accuracy = Accuracy.Fine };
string bestProvider = _locationManager.GetBestProvider(criteria, true);
Location lastKnownLocation = _locationManager.GetLastKnownLocation(bestProvider);
//LOG RECORD FOR CURRENT LOCATION
if (lastKnownLocation != null)
{
PostData(lastKnownLocation, DateTime.Now);
}
});
}
public void StopTracking()
{
isTracking = false;
_locationManager.RemoveUpdates(this);
BusTimer.Stop();
}
#endregion
#region POST LOCATION DATA TO SERVER
private void PostData(Location setLocation, DateTime setDate)
{
try
{
currentLocation = setLocation;
string longString = string.Format("{0}", setLocation.Longitude);
string latString = string.Format("{0}", setLocation.Latitude);
//POST DATA OVER
var client = new RestClient("http://mywebserver/");
var request = new RestRequest("API/DataAPI/PerformAction", Method.GET);
request.AddParameter("ActionName", "PostEntityData"); // adds to POST or URL querystring based on Method
string JSONString = "{'EntityID':" + currentAsset.AssetID.ToString() + ",'EntityName':'Asset','Long':" + longString + ",'Lat':" + latString + ", 'CheckDateTime':'" + setDate.ToString("yyyy-MM-dd HH:mm:ss") + "'}";
request.AddParameter("ActionData", JSONString); // adds to POST or URL querystring based on Method
client.ExecuteAsync(request, response =>
{
Console.WriteLine(response.Content);
this.RunOnUiThread(new Runnable(PostToastData));
});
}
catch (System.Exception e)
{
Console.Write(e.Message);
}
}
public void PostToastData()
{
string longString = string.Format("{0}", currentLocation.Longitude);
string latString = string.Format("{0}", currentLocation.Latitude);
Toast.MakeText(this, "Logged Location record for " + currentAsset.AssetNumber + " at Long: " + longString + ", Lat: " + latString, ToastLength.Short).Show();
}
#endregion
これはすべて完璧に機能します。また、アプリを最小化すると、データがサービスに正しく送信され続けます。問題は、約 1 時間後にアプリが突然停止することです。
一定時間後にクラッシュする理由は誰にもわかりますか? アプリケーションはRAMをほとんど使用していないため、メモリの問題ではないと思います。バックグラウンドで実行されているアプリを別のマナーで処理する必要がありますか? アプリがフォーカスされていないときにバックグラウンドで実行し続けることができるように、このプロセスを別のスレッドで実行する必要がありますか?
アドバイスをいただければ幸いです。
ありがとう