首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android后台服务

Android后台服务
EN

Stack Overflow用户
提问于 2015-06-02 10:08:14
回答 1查看 628关注 0票数 3

我想在我的钛应用程序中使用后台服务。

我使用了所有需要插入到钛Android应用程序中的代码。

在TiApp.xml文件中:这里的注册服务

代码语言:javascript
复制
<services>
    <service url='BGServ.js' type="interval"/>
</services>

这里"BGServ.js“文件放在我的app/lib文件夹中。

在BGServ.js文件中:我们的服务文件代码

代码语言:javascript
复制
var service = Titanium.Android.currentService;
var intent = service.intent;
Ti.API.info('Background Service Started');
Ti.API.info('Background Service of Platform : ' + Titanium.Platform.osname + ' Started');

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

在index.js文件中:我们在这里创建服务意图

代码语言:javascript
复制
var intent = Titanium.Android.createServiceIntent({
	url : '/BGServ.js'
});
intent.putExtra('interval', 1000);
intent.putExtra('message_to_echo', 'Test Service');
Titanium.Android.startService(intent);

问题:Android没有在我的项目中工作。在BGServ.js中,有一个也没有在控制台中打印的Ti.API.info()。我在这里,帮帮我。

TiSDK版本:3.5.0GA

谢谢,

亚比乌珊

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-17 09:45:11

在钛Appcelerator中找到了安卓后台服务的解决方案

在TiApp.xml文件中:

代码语言:javascript
复制
<services>
  <service type="interval" url="bg_service.js"/>
</services>

注意到在url属性中不需要在文件名之前添加"\"。

并将后台服务文件仅放在应用程序上的"assets“文件夹中。

在"bg_service.js“文件中:我们的服务文件代码

代码语言:javascript
复制
var service = Titanium.Android.currentService;
var intent = service.intent;
Ti.API.info('Background Service Started');
Ti.API.info('Background Service of Platform : ' + Titanium.Platform.osname + ' Started');

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

在Index.xml文件中:我们在这里创建意图并启动后台服务

代码语言:javascript
复制
var intent = Titanium.Android.createServiceIntent({
  url : 'bg_service.js'
});
intent.putExtra('interval', BG_INTERVAL_SECONDS);

Titanium.Android.startService(intent);

注意到在url属性中不需要在文件名之前添加"\"。

代码语言:javascript
复制
Issue Resolved by above code and also take care of **"\"** in url attribute

如果有人找到其他解决方案,那就在这里重放一遍。

谢谢,

亚比乌珊

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30593098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档