简体   繁体   English

如何使用appcelerator构建服务Android

[英]How to build service Android with appcelerator

I'm building a service with appcelerator studio. 我正在用appcelerator工作室建立一个服务。 This is the code that I have building in Index.js page: 这是我在Index.js页面中构建的代码:

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();

Now when I try to start the application, the myservice.js is execute every 60 second, but I have two problem: 现在,当我尝试启动应用程序时, myservice.js每60秒执行一次,但我有两个问题:

1) when the service is running, I have a performance problem with interface 2) If I close the application, the service is not running. 1)当服务运行时,我的接口存在性能问题2)如果我关闭应用程序,则服务未运行。

So how can I implement a service with appcelerator that run in background also when the app is not running? 那么,当应用程序未运行时,如何在后台运行appcelerator实现服务?

I tried this once: http://docs.appcelerator.com/platform/latest/#!/guide/Android_Services-section-43287937_AndroidServices-ServiceCode and seems to work fine. 我试过一次: http//docs.appcelerator.com/platform/latest/#!/guide/Android_Services-section-43287937_AndroidServices-ServiceCode ,似乎工作正常。 To log even with the app closed, you should use ADB logcat. 要在关闭应用程序时进行记录,您应该使用ADB logcat。 Also, don't forget to include it in your tiapp.xml like: 另外,不要忘记将它包含在tiapp.xml中,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM