It is advisable to use thread/asynTask or Service to handle long running task such as file I/O, internet access etc. IntentService is simple, easy to use and take care of many hectic tasks for You.Read more about IntentService at
developer.android . A simple use case of the topic can be:
Lets start, First we need to have our own receiver class extended from ResultReceiver and containing Receiver Interface. Lets say its MyResultReceiver:
Now we implements Receiver interface [defined in MyResultReceiver] in our Activity. Lets say this LoginActivity :
And here is the MyService class :
Whenever we call startService , intentService is started if its not been running, and if its already running it put our (new)request in a queue and execute it when it finishes running request. When we call rec.send() , onReceiveResult is called and the value we passed in bundle is received in the activity.
developer.android . A simple use case of the topic can be:
- Your activity send a web request to IntentService to process.
- Your IntentService execute that request using DefaultHttpClient.
- And Return results (whatever, true,false list of objects/records etc) back to the calling activity.
- Broadcast should be used if you want to send data/notifications across applications, whenever you send broadcast its sent system wide read more about broadcast receivers atdeveloper.android.
- Result receiver is an interface you implement and pass it to the intentService through putExtra. IntentService then fetch this object and call its receiver.send function to send anything (in bundle) to calling activity[who started the intentservice]. Result receiver has preference over broadcast receivers if your all communication is internal to your application.
Lets start, First we need to have our own receiver class extended from ResultReceiver and containing Receiver Interface. Lets say its MyResultReceiver:
package sohail.aziz.service; import android.os.Bundle; import android.os.Handler; import android.os.ResultReceiver; public class MyResultReceiver extends ResultReceiver { private Receiver mReceiver; public MyResultReceiver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } }
Now we implements Receiver interface [defined in MyResultReceiver] in our Activity. Lets say this LoginActivity :
package sohail.aziz.view; import sohail.aziz.service.MyResultReceiver; import sohail.aziz.service.MyIntentService; import sohail.aziz.service.MyResultReceiver.Receiver; public class LoginActivity extends Activity implements OnClickListener,Receiver { Button btLogin; Public MyResultReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); btLogin = (Button) findViewById(R.id.btLogin); mReceiver = new MyResultReceiver(new Handler()); mReceiver.setReceiver(this); btLogin.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(arg0.getId()==R.id.btLogin){ Intent i = new Intent(this, MyIntentService.class); i.putExtra("nameTag","sohail" ); i.putExtra("receiverTag", mReceiver); startService(i); } } @Override public void onReceiveResult(int resultCode, Bundle resultData) { // TODO Auto-generated method stub Log.d("sohail","received result from Service="+resultData.getString("ServiceTag")); } }
And here is the MyService class :
import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.os.ResultReceiver; import android.util.Log; public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub ResultReceiver rec = intent.getParcelableExtra("receiverTag"); String recName= intent.getString("nameTag"); Log.d("sohail","received name="+recName); Log.d("sohail","sending data back to activity"); Bundle b= new Bundle(); b.putString("ServiceTag","aziz"); rec.send(0, b); } }
Whenever we call startService , intentService is started if its not been running, and if its already running it put our (new)request in a queue and execute it when it finishes running request. When we call rec.send() , onReceiveResult is called and the value we passed in bundle is received in the activity.