gpt4 book ai didi

javascript - 如何使用appcelerator构建Android服务

转载 作者:行者123 更新时间:2023-11-28 05:43:20 25 4
gpt4 key购买 nike

我正在使用 appcelerator studio 构建一项服务。这是我在 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();

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

1) 当服务运行时,我遇到了接口(interface)性能问题2)如果我关闭应用程序,则服务不会运行。

那么如何使用 appcelerator 实现一个在应用程序未运行时也在后台运行的服务?

最佳答案

我尝试过一次: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>

关于javascript - 如何使用appcelerator构建Android服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com