Android Background Service

657 Views Asked by At

I want to use background Service in my Titanium Application.

I used every code which need to insert in Titanium Android application.

In TiApp.xml file : Register Service here

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

Here "BGServ.js" file is placed in my app/lib folder.


In BGServ.js file : Our Service file code

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


In index.js file : Here we creating 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);


Issue : Android Service is not working in my project. In BGServ.js having a Ti.API.info() which also not printed in console. I am struct here help me.

TiSDK Version : 3.5.0 GA

Thanks,

Abidhusain

1

There are 1 best solutions below

0
On BEST ANSWER

Found the Solution of Background Service for Android in Titanium Appcelerator

In TiApp.xml file :

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

Note that here in url attribute no need to add "\" ahead of file name.

And also put Background Service file in only "assets" folder on your application.

In "bg_service.js" file : Our Service file code

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

In Index.xml file : Here we creating intent and start background Service

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

Titanium.Android.startService(intent);

Note that here in url attribute no need to add "\" ahead of file name.

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

If anyone found any other solution then replay here.

Thanks,

Abidhusain