Develop own services in Android using Titanium Framework
Posted By : Sapna Sharma | 02-Mar-2015

What are services and why we use them ?
A service is a component which runs in the background without direct interaction with the user. As the service has no user interface, it is not bound to the lifecycle of an activity.
Services are used for repetitive and potentially long running operations, i.e., Internet downloads, checking for new data, data processing, updating content providers and the like.
Defining and starting a custom services
Implementation and declaration
File: myservice.js:
var service = Titanium.Android.currentService;
var intent = service.intent;
var message = intent.getStringExtra("message");
Titanium.API.info("Hello! I am a Service. I have this to say: " + message)
Register the service in tiapp.xml:
Start a service
An Android component (service, receiver, activity) can be used to start a service.
var intent = Titanium.Android.createServiceIntent({
url : 'myservice.js'
});
// Service should run its code every 10 seconds
intent.putExtra('interval' , 10*1000);
//// A message that the service should repeat
intent.putExtra('message', 'Titanium rocks!');
Titanium.Android.startService(intent);
Stopping a service
No matter how frequently you called the startService(intent) method, one call to the stopService() method stops the service.
var intent = Titanium.Android.createServiceIntent({
url : 'myservice.js'
});
Titanium.Android.stopService(intent);
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sapna Sharma
Sapna is a bright Android Apps developer using Titanium framework. Sapna likes music and helping needy people.