2016-08-03 77 views
1

我正在使用appcelerator工作室构建服务。这是我在建设Index.js代码页:如何使用appcelerator构建服务Android

var intent = Titanium.Android.createServiceIntent({ url: 'myservice.js' }); 
// Service should run its code every 2 seconds. 
intent.putExtra('interval', 60000); 
// A message that the service should 'echo' 
//intent.putExtra('message_to_echo', 'Bo uscirà questo messaggio?'); 

var service = Titanium.Android.createService(intent); 
service.addEventListener('resume', function(e) { 
    Titanium.API.info('Service code resumes, iteration ' + e.iteration); 
}); 
service.addEventListener('pause', function(e) { 
    Titanium.API.info('Service code pauses, iteration ' + e.iteration); 
    if (e.iteration === 1) { 
     var _model = Alloy.createModel("ServiceDAO", { 
      ID : 1, 
      ServiceRunning: 0, 
      ApplicationRunning: 0 
     }); 
    _model.save();  
    } 
}); 
service.start(); 

现在,当我尝试启动应用程序时,myservice.js是执行每60秒,但我有两个问题:

1)当服务运行时,我有一个接口的性能问题 2)如果我关闭应用程序,服务没有运行。

那么我怎么能实施一个服务与appcelerator运行在后台也应用程序不运行?

+0

你可能需要建立为 –

+0

模块你能解释一下我怎样才能建立为 – bircastri

+0

HTTPS模块:// wiki.appcelerator.org/display/guides2/Android+Module+Development+Guide –

回答

0

我试过一次:http://docs.appcelerator.com/platform/latest/#!/guide/Android_Services-section-43287937_AndroidServices-ServiceCode,似乎工作正常。 要在应用程序关闭时进行日志记录,则应使用ADB logcat。 另外,不要忘了把它列入你的tiapp.xml这样的:

<services> 
    <service url="your_service_name.js" type="interval"/> 
</services> 
+0

我刚刚在我的Tiapp.xml中插入了这段代码 而且我的服务能够正常工作,但是只有当应用程序在昨天运行 – bircastri

+0

时,我发现它需要有一点让ADB在我杀了应用程序后记录我的代码。我每10秒钟都有一个日志,但是当我关闭我的应用程序时,大约需要2分钟,而不是每10秒钟重新开始一次。 顺便说一句,我正在使用'''adb logcat | grep“TiAPI”'''仅记录Titanium API –