I have one subclass of, one subclass of and one subclass(MyAsyncTask) of android.os.AsyncTask.

Sample Code:

onCreate(...) { // start MyService }

onStartCommand(...){ // execute MyAsyncTask }

onPostExecute(String result) { // send result to MyActivity};

My question is, how can i send results from MyAsyncTask when success to MyActivity.

No correct solution


This is not easy to answer in a short paragraph. But basically what you want to do is use binding for your service.

It's certainly not a trivial task, but it's the elegant way to go. Besides of initiating your service with startService() you would additionally bind to your service. The binding mechanism allows your Activity to have a pointer to your Service class. With this pointer you can do whatever you want, including passing a pointer of the Activity itself to the Service, so the service would have a pointer to your Activity.

Once the Service has a pointer to your Activity, you can call any methods and set any variables you want, including setting the return value from your AsyncTask.

You can make a SingleInstance model with the MyActivity.

class MyActivity extends Activity{
  private static MyActivity instance;
  public static MyActivity getInstance(){
      return instance;
  public void onCreate(Bundle savedInstanceState) {
      instance = this;


  public void onDestroy() {
       instance = null;

  public void doSomething(){}

Then you can call MyActivity.getInstance().doSomething();inside the onPostExecute

Some alternatives:

  • use an IntentService, which is basically a Service that handles asynchronous requests

  • send a broadcast from your AsyncTask's onPostExecute and receive it in you Activity (simple example here)

I have tried this structure and its works on my app. Hope its helpful. Create the service class extended with async task and singleton ..

 <service android:name=".myService" />

Service class

public class myService extends AsyncTask<String,String,String> 
    private Activity active  = null;
    protected myService () {}

     public static myService getInstance(Activity activity) {
      myService  myTask = new myService (); = activity;
      return myTask;

   public startTask()  {
     //start the async task.. 
     this.execute(new String[] {});

  protected void onPostExecute(String result) { 

    //check result sucess cond and call


Activity class

 protected void onCreate(Bundle savedInstanceState) {

  public void updateActivity(result){
     //do something on activity with result...

You can send result from service( and class to activity(myactivity) class through "Broadcast"

here is an example

in your onPostExecute(String result) method add those:

Intent intent = new Intent("your  activity class[]");
intent.putExtra("RESULT", result)

and your myactivity class receive sending broadcast like this:

private BroadcastReceiver receiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String bundle = intent.getStrings();// get resulting string and do whatever you want.


don't forget to register and unregister broadcast in myactivity class, like:

protected void onResume() {
    registerReceiver(receiver, new IntentFilter("your service class"));

protected void onPause() {

A way is to Bind your Activity to your service so that they can share a common communication channel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow