2014-04-04 78 views
0

在我的iOS应用程序中,我使用setInterval获取当前位置我能够获取当前位置。钛工作室使用后台服务获取当前位置?

我的问题是,即使我的应用程序在进入后台时也需要获取当前位置并保存到数据库中?

会有人指导我如何做到这一点?

我在app.js

Ti.App.addEventListener('pause', function(e) { 
     // register a background service. this JS will run when the app is backgrounded 
    //checking that switch is ON/OFF 
     if (Ti.App.Properties.getString('getLocation') == 'YES') { 
     //timer = setInterval(Titanium.Geolocation.addEventListener('location', locationCallBack1), 10000); 
     service = Ti.App.iOS.registerBackgroundService({ 
      url : '/mainWindows/bg.js' 
     }); 
     } 

}); 

在此先感谢代码。

回答

0

你可能想在Appcelerator的论坛,看看这个帖子在:Location tracking in the background with iOS

它利用iOS的后台服务,这是在docs很好的解释:

// The app is transtioning to background here 
Ti.App.addEventListener('pause', function(e){ 
    // Register background service 
    bgService = Ti.App.iOS.registerBackgroundService({ url: 'bgService.js' }); 
    Ti.API.info("registered background service = "+ bgService); 
}); 
+0

mwfire ,谢谢你的回应,即使我遵循了你给出的相同网址。在我的应用程序中,我有一个窗口来启用/禁用位置,为此,我使用了开关ON/OFF。如果我在同一个屏幕上获得纬度和长度值,甚至选择这个窗口,如果我最小化了我的应用程序,它正在工作。当我跳转到其他窗口/屏幕或其他选项卡。我没有获得纬度和长期价值,请你帮我解决这个问题。在此先感谢 –

+0

您可能必须显式更新您的tiapp.xml以启用更长时间的Geolocation而不关闭。你可以在这里找到更多的细节:http://docs.appcelerator.com/titanium/3.0/#!/guide/tiapp.xml_and_timodule.xml_Reference-section-29004921_tiapp.xmlandtimodule.xmlReference-UIBackgroundModes '位置' 设我知道它是否有效! – mwfire

+0

此外,设置'Ti.Geolocation.trackSignificantLocationChange = true;'可能是更好的方法来跟踪背景中的地理位置。 http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Geolocation-property-trackSignificantLocationChange – mwfire