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

(Android) Get GPS location while App is closed with background service

$
0
0

Hey there,

I'm currently working with getting GPS location on Android so that I can send coordinates to a server, using a background service.

It works great with the app opened as well as with it "minimized". The problem is when I close it completely. If I go to the running apps screen I can see my service is running alongside its instructions, but I'm no longer able to get my GPS location.

After some research, I managed to find a new way to build my service, but doing so left me wihtout GPS as I can't find a way to merge the two.

Old Service:

/*  lOCATION PROVIDERS  */
var gps_provider = Titanium.Geolocation.Android.createLocationProvider({
    name: Titanium.Geolocation.PROVIDER_GPS,
    minUpdateTime: 10,
    minUpdateDistance: 100
});
Titanium.Geolocation.Android.manualMode = true;
Titanium.Geolocation.Android.addLocationProvider(gps_provider);
 
Titanium.Geolocation.addEventListener('location', handler_location);
 
 
function handler_location(e) {
 
    Titanium.API.info('handler location');
    Titanium.App.fireEvent("test", {data: e});
 
}
New Service:
var intent = Ti.Android.createIntent({
    flags: Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
    action: 'android.settings.LOCATION_SOURCE_SETTINGS'
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
 
var pending = Titanium.Android.createPendingIntent({
    intent: intent,
    flags: Titanium.Android.FLAG_UPDATE_CURRENT
});
 
// create a random id
var nId = parseInt(10000 * Math.random());
 
var url = "http://10.0.3.2:32652/test";
//var url = "http://192.168.0.112:32652/test";
var params = {
    lat: 'latitude',
    lng: 'longitude'
};
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload : function(e) {
        var result = JSON.parse(this.responseText);
        // Create the notification
        var notification = Titanium.Android.createNotification({
            contentTitle: result.data.title,
            contentText : result.data.text,
            contentIntent: pending
        });
        // Send the notification.
        Ti.Android.NotificationManager.notify(nId, notification);
 
        Ti.API.info("Received text: " + this.responseText);
    },
    // function called when an error occurs, including a timeout
    onerror : function(e) {
        Ti.API.debug(e.error);
    },
    timeout : 5000  // in milliseconds
});
 
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send(params);
So, recapping: I had a background service that would give me GPS coordinates. I later changed to what I believe is a more efficient way of structuring said service. In either approach I was able to get my GPS location once the app was closed.

Thanks in advance!


Viewing all articles
Browse latest Browse all 8068

Trending Articles



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