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

 

About Author

Author Image
Sapna Sharma

Sapna is a bright Android Apps developer using Titanium framework. Sapna likes music and helping needy people.

Request for Proposal

Name is required

Comment is required

Sending message..