Today, i want to show you how services works. They can be used to make some things at regular intervals of time. You can check for updates of data, gps location, and other.
A service must be declared in .Manifest like an Activity.
<service android:name=".MyService"/>
Code must contain a Timer and class MyService must extends Service like in the example:
public class MyService extends Service { private Timer t1 = new Timer(); protected void onCreate() { super.onCreate(); startingservice(); } }
Now i will show you the method startingservice().
private void startingservice() { t1.scheduleAtFixedRate( new TimerTask() { public void run() { //Your task repeated at "INTERVAL" } }, 0, INTERVAL); ; }
At every INTERVAL(that is expressed in seconds) your task is executed by this service that runs in background once the service is started.
If you want to kill this service you will need the following method.
You can call this method on the click of a button, or after certain time when you don’t need the service anymore.
You can call it also at onDestroy() method, that means the service will stop once the app will be closed.
I will just show you the method stopingservice()
private void stopservice() { if (t1 != null){ t1.cancel(); } }


