0

我正在尝试制作一个简单的跟踪应用程序。它所做的只是注册位置更新,然后每 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

这一切都完美无缺。当我最小化应用程序时,它会继续正确地将数据发布到服务中。问题是大约一个小时后,应用程序意外地死掉了。

任何人都可以看到它在一定时间后崩溃的任何原因吗?该应用程序几乎没有使用任何内存,所以我认为这不会是内存问题。我应该在不同的庄园处理后台运行的应用程序吗?我是否应该在不同的线程上执行此过程,以便在应用程序不在焦点时它可以继续在后台执行?

任何意见,将不胜感激。

谢谢

4

1 回答 1

2

如果你想在你的应用失去焦点后继续在后台运行,你应该使用后台服务。以下是一些可以帮助您入门的链接:

于 2012-12-10T15:25:12.570 回答