【问题标题】:Android Background Service安卓后台服务
【发布时间】:2015-08-16 01:40:08
【问题描述】:

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

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

在 TiApp.xml 文件中:在此处注册服务

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

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


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

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 文件中:这里我们创建 Service Intent

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() 也没有在控制台中打印。我是 struct 在这里帮帮我。

TiSDK 版本:3.5.0 GA

谢谢,

阿比杜塞恩

【问题讨论】:

    标签: javascript android titanium background-service titanium-android


    【解决方案1】:

    在Titanium Appcelerator中找到了Android后台服务的解决方案

    在 TiApp.xml 文件中:

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

    注意这里在url属性中不需要在文件名前加"\"

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

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

    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 文件中:这里我们创建意图并启动后台服务

    var intent = Titanium.Android.createServiceIntent({
      url : 'bg_service.js'
    });
    intent.putExtra('interval', BG_INTERVAL_SECONDS);
    
    Titanium.Android.startService(intent);

    注意这里在url属性中不需要在文件名前加"\"

    Issue Resolved by above code and also take care of **"\"** in url attribute
    

    如果有人找到任何其他解决方案,请在此处重播。

    谢谢,

    阿比杜塞恩

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多