This article explain the usage of IntentService class in android SDK to perform background processing to offload tasks from an application’s main thread (Your activity).
when IntentService jobs done, the results are sent back to the activity.
Good example whould be an application that uses background processing for REST calls; to download some data from the internet, parse it, and send it back to the application.
There are several ways to do background (asynchronous) processing tasks:
- AsyncTask
- Service (or IntentService)
- Implement your own Threading mechanism
while the 3rd options is too compilcated for this purpose you should consider one of the others generaly.
ASyncTask enables proper and easy use of the UI thread.
A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand.
With AsyncTask if the user goes to another Activity you can’t transfer that object to the other Activity so it dies.
You can communicate between Service and Activity, but it’s tricky to do it right using Binders.
Contents
IntentService
Class Overview:
- Since: API Level 3
- IntentService is a base class for Services that handle asynchronous requests
- Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
- All requests are handled on a single worker thread — they may take as long as necessary (and will not block the application’s main loop), but only one request will be processed at a time.
You should read more on the Class overview at developer.android.com.
Class Usage:
To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
Base IntentService
Before I will continue with a full example of using IntentService you can look at how should a base IntentService looks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import android.app.IntentService; import android.content.Intent; public class Downloader extends IntentService { public Downloader(String name) { super(name); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent arg0) { // TODO Auto-generated method stub } } |
import android.app.IntentService; import android.content.Intent; public class Downloader extends IntentService { public Downloader(String name) { super(name); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent arg0) { // TODO Auto-generated method stub } }
- IntentService has a single constructor that takes a string argument
“name“. It’s only use is in naming the
worker thread for the IntentService. - onHandleIntent(): This method is invoked on the worker thread with a request to process.
- You should not override onStartCommand method for your IntentService.
IntentService Example
BgProcessingActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package itekblog.examples; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.widget.Toast; public class BgProcessingActivity extends Activity implements BgProcessingResultReceiver.Receiver { public BgProcessingResultReceiver mReceiver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(this, "Starting background processing...", Toast.LENGTH_LONG).show(); // register a reciever for IntentService broadcasts mReceiver = new BgProcessingResultReceiver(new Handler()); mReceiver.setReceiver(this); // start IntentService final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, BgProcessingIntentService.class); // optional: send Extra to IntentService // intent.putExtra("name", "value"); intent.putExtra("receiver", mReceiver); intent.putExtra("command", "query"); startService(intent); } @Override public void onReceiveResult(int resultCode, Bundle resultData) { switch (resultCode) { case BgProcessingIntentService.STATUS_RUNNING: //show progress Toast.makeText(this, "Running", Toast.LENGTH_LONG).show(); break; case BgProcessingIntentService.STATUS_FINISHED: // hide progress & analyze bundle Toast.makeText(this, "OK", Toast.LENGTH_LONG).show(); break; case BgProcessingIntentService.STATUS_ERROR: // handle the error; Toast.makeText(this, "Error", Toast.LENGTH_LONG).show(); break; } } @Override public void onPause() { super.onPause(); if (mReceiver!=null) mReceiver.setReceiver(null); // clear receiver to avoid leaks. } } |
package itekblog.examples; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.widget.Toast; public class BgProcessingActivity extends Activity implements BgProcessingResultReceiver.Receiver { public BgProcessingResultReceiver mReceiver; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(this, "Starting background processing...", Toast.LENGTH_LONG).show(); // register a reciever for IntentService broadcasts mReceiver = new BgProcessingResultReceiver(new Handler()); mReceiver.setReceiver(this); // start IntentService final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, BgProcessingIntentService.class); // optional: send Extra to IntentService // intent.putExtra("name", "value"); intent.putExtra("receiver", mReceiver); intent.putExtra("command", "query"); startService(intent); } @Override public void onReceiveResult(int resultCode, Bundle resultData) { switch (resultCode) { case BgProcessingIntentService.STATUS_RUNNING: //show progress Toast.makeText(this, "Running", Toast.LENGTH_LONG).show(); break; case BgProcessingIntentService.STATUS_FINISHED: // hide progress & analyze bundle Toast.makeText(this, "OK", Toast.LENGTH_LONG).show(); break; case BgProcessingIntentService.STATUS_ERROR: // handle the error; Toast.makeText(this, "Error", Toast.LENGTH_LONG).show(); break; } } @Override public void onPause() { super.onPause(); if (mReceiver!=null) mReceiver.setReceiver(null); // clear receiver to avoid leaks. } }
BgProcessingIntentService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package itekblog.examples; import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.os.ResultReceiver; import android.util.Log; public class BgProcessingIntentService extends IntentService { public static final int STATUS_RUNNING = 0; public static final int STATUS_FINISHED = 1; public static final int STATUS_ERROR = 2; public BgProcessingIntentService() { super(BgProcessingIntentService.class.getName()); } @Override protected void onHandleIntent(Intent arg0) { Log.d(BgProcessingIntentService.class.getName(), "service started..."); // sendBroadcast(); final ResultReceiver receiver = arg0.getParcelableExtra("receiver"); String command = arg0.getStringExtra("command"); Bundle b = new Bundle(); if(command.equals("query")) { receiver.send(STATUS_RUNNING, Bundle.EMPTY); try { // get some data or something //b.putParcelableArrayList("results", results); receiver.send(STATUS_FINISHED, b); } catch(Exception e) { b.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, b); } } Log.d(BgProcessingIntentService.class.getName(), "service stopping..."); this.stopSelf(); } } |
package itekblog.examples; import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.os.ResultReceiver; import android.util.Log; public class BgProcessingIntentService extends IntentService { public static final int STATUS_RUNNING = 0; public static final int STATUS_FINISHED = 1; public static final int STATUS_ERROR = 2; public BgProcessingIntentService() { super(BgProcessingIntentService.class.getName()); } @Override protected void onHandleIntent(Intent arg0) { Log.d(BgProcessingIntentService.class.getName(), "service started..."); // sendBroadcast(); final ResultReceiver receiver = arg0.getParcelableExtra("receiver"); String command = arg0.getStringExtra("command"); Bundle b = new Bundle(); if(command.equals("query")) { receiver.send(STATUS_RUNNING, Bundle.EMPTY); try { // get some data or something //b.putParcelableArrayList("results", results); receiver.send(STATUS_FINISHED, b); } catch(Exception e) { b.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, b); } } Log.d(BgProcessingIntentService.class.getName(), "service stopping..."); this.stopSelf(); } }
BgProcessingResultReceiver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package itekblog.examples; import android.os.Bundle; import android.os.Handler; import android.os.ResultReceiver; public class BgProcessingResultReceiver extends ResultReceiver { private Receiver mReceiver; public BgProcessingResultReceiver(Handler handler) { super(handler); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } } |
package itekblog.examples; import android.os.Bundle; import android.os.Handler; import android.os.ResultReceiver; public class BgProcessingResultReceiver extends ResultReceiver { private Receiver mReceiver; public BgProcessingResultReceiver(Handler handler) { super(handler); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } }
AndroidManifest.xml
1 | <!--?xml version="1.0" encoding="utf-8"?--> |
<!--?xml version="1.0" encoding="utf-8"?-->
don’t forget to register both files in your AndroidManifest.xml:
this method should work perfectly and the REST call is running in a background process, updating the activity when done.
Development Specialist, Artist and Activist
Personal Website
Great Post! Thanks a lot!
Finally! Someone does it properly! I HATE BroadcastReceivers!
Hey,this article was very useful , bur when I try register my ResultReceiver.java to the manifest it raises the error :
com.example.malwareguard.malwareguard.MyResultReceiver’ has no default constructor
Validates resource references inside Android XML files.
‘com.example.malwareguard.malwareguard.MyResultReceiver’ is not assignable to ‘android.content.BroadcastReceiver’
Validates resource references inside Android XML files.
I wrote :
Do you have an idea ?
I wrote :
Sorry,- receiver android:name=”.MyResultReceiver”