This is more of a beginners entry about that topic. Background tasks are called Service in Android. Creating and using one is pretty simple. From your activity call the following code:

[codesyntax lang=“java“ lines=“normal“]

public class TestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startService(new Intent(this, TestService.class));
    }
}

[/codesyntax]

And the service class looks like this for instance:

[codesyntax lang=“java“ lines=“normal“]

public class TestService extends Service{
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);

		new Thread(new Runnable() {

			public void run() {

				while(1 == 1){
				// do something useful and dont waste to much battery
				}

			}
		}).start();
	}
}

[/codesyntax]

Thats basically it, all you have to do now to make it work, is register that service within the AndroidManifest.xml with a line like that:

[codesyntax lang=“xml“]

<service android:name=".package.TestService" />

[/codesyntax]

Now you are done and got a background job running on your android device, again, easy, isn’t it?