Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Titanium Android GPS Tracking Background Service

$
0
0

Hi, I have some quiz. how to coding a GPS Tracking runing in background service and Start/Stop when press switch in app? My GPS Tracking stop after 5 minutes. Please Help me.

tracksgps.js -- Tracking Service

var service = Ti.Android.currentService;
var intent = service.getIntent();
 
var deviceUrl = "http://112.121.134.78/tracks/api/devices";
var trackUrl = "http://112.121.134.78/tracks/api/trackers?deviceid=";
 
var DeviceTag = Titanium.Platform.id;
var deviceid, TrackLat, TrackLng;
 
var chkDevice = Titanium.Network.createHTTPClient();
var trackDevice = Titanium.Network.createHTTPClient();
 
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.distanceFilter = 10;
 
if (Ti.Geolocation.locationServicesEnabled) {
    Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
    Titanium.Geolocation.addEventListener('location', function(e) {
        if (e.error) {
            alert('Error: ' + e.error);
        } else {
            TrackLat = e.coords.latitude;
            TrackLng = e.coords.longitude;
 
            chkDevice.open('GET', deviceUrl + '?deviceTag=' + DeviceTag);
            chkDevice.send();
        }
    });
}
 
chkDevice.onload = function(e) {
    var res = this.responseText;
    var Res = JSON.parse(res);
 
    if (Res != null) {      
        trackDevice.open('POST', trackUrl + Res.DeviceId);
        trackDevice.send({
            TrackLat : TrackLat,
            TrackLng : TrackLng
        });
    }
};
chkDevice.onerror = function(e) {
    alert('On Check Device Error :' + e.error);
};
 
trackDevice.onload = function(e) {
    alert('Tracking');
};
trackDevice.onerror = function(e) {
    alert('On Track Device Error :' + e.error);
};
servicetracks.js -- call gps tracking
var serviceUrl = "tracksgps.js";
var SECS = 20;
 
$.swTrack.addEventListener('change', function(e) {
    if (this.value == true) {
        try {
            var intent = Ti.Android.createServiceIntent({
                url : serviceUrl
            });
            //intent.putExtra('interval', SECS * 1000);
            Ti.Android.startService(intent);
        } catch(e) {
            alert(e);
        }
        //alert('a');
    } else {
        try {
            var intent = Ti.Android.createServiceIntent({
                url : serviceUrl
            });
            Ti.Android.stopService(intent);
        } catch(e) {
            alert(e);
        }
        //alert('b');
    }
});

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>