How to use Android Services

Tutorials with advanced 'difficulty' and more Lines of Code.

How to use Android Services

Postby itsricardo881 » Mon Apr 05, 2010 8:44 am

Hey guys,

This is my first article on android...
I chose this as the first coz this is where i spent time doing things wrong and trying to make it better!!

Android provides a very convenient way to implement services as you will see.

Any service class that you need to have must extend the Android's Service class and should override certain methods and typically should look as follows.

public class MyService extends Service {
private static final String LOG_TAG = "MySERVICE"; //$NON-NLS-1$
Handler serviceHandler = null; //To handle the service

@Override
public void onStart( Intent intent, int startId ) {
super.onStart( intent, startId );
Log.d( LOG_TAG, "onStart" ); //$NON-NLS-1$


// add the code that you need to do on the start of the service

this.serviceHandler = new Handler();


// if you want certain task to be done periodically define a task and call it this way
this.serviceHandler.postDelayed( new RunTask(), 2000L );

}



@Override
public IBinder onBind( Intent intent ) {
//return binder;


// you might as well define an binder class and use it to bind to a running activity
//

return null;
}



@Override
public void onCreate() {
super.onCreate();
Log.d( LOG_TAG,"onCreate" ); //$NON-NLS-1$
}

@Override
public void onDestroy() {

super.onDestroy();
Log.d( LOG_TAG,"onDestroy" ); //$NON-NLS-1$
}


class RunTask implements Runnable {

/**
* Operations to be performed when the e-mail service is running
*/
private void doServiceWork(){

Log.d( LOG_TAG,"Entered the service work" ); //$NON-NLS-1$

// add your service code here
}



/**
* Does the job of the service
*/
public void run() {

doServiceWork();


// to have the service task run after 2 seconds periodically
serviceHandler.postDelayed( this, 2000L );


}



}
}


So far so good. You can define the service and use it as follows from any activity


Intent i = new Intent();
i.setClassName( "com.Test.UI",
"com.Test.UI.MyService" );
startService( i );


But having the service this way will result in having the service running all the time and faster battery down. what u can do is stop as and when u are done.


Intent i = new Intent();
i.setClassName( "com.Test.UI",
"com.Test.UI.MyService" );
stopService( i );

In most of the cases we create a service to have it triggered at a particular point of time. So having the service running in the background and checking for
the time intended is an option, but ineffective. So services can be hooked up to what are called alarms provided by android.

U can have the service started at any particular point of time this way



AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Intent i = new Intent(getApplicationContext(), MyService.class);
PendingIntent pendingIntent =
PendingIntent.getService(getApplicationContext(), 0, i, 0);


Calendar calendar = Calendar.getInstance();

Calendar calendarTimetoStart = calendar.clone();

// set the calendar to the time u want the service to start

calendarTimetoStart.set(Calendar.HOUR_OF_DAY, 3);
calendarTimetoStart.set(Calendar.MINUTE, 59);
calendarTimetoStart.set(Calendar.SECOND, 0);

long diffInTime = calendarTimetoStart.getTimeInMillis() - calendar.getTimeInMillis();


long firstWake = System.currentTimeMillis() + diffInTime;

am.set(AlarmManager.RTC, firstWake, pendingIntent);

In addition to this if u want the alarm to be set periodically, what you can do is on every start of the service (OnStart), create an alarm and set it again in

the same way. Once u are done with the service work, call stopSelf() in the doServiceWork method.

Hope that helped.
Any suggestions and improvements from the experts are most welcome :)
itsricardo881
Freshman
Freshman
 
Posts: 2
Joined: Mon Feb 15, 2010 3:02 pm

Top

Postby anonymous_dude » Tue Apr 20, 2010 1:32 pm

Hi itsmeRicardo881 :P

Mujhe itna accha explain nhi kiya! :P
anonymous_dude
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Apr 20, 2010 1:28 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests