Pergunta

I have one subclass of android.support.v4.app.FragmentActivity(MyActivity), one subclass of android.app.Service and one subclass(MyAsyncTask) of android.os.AsyncTask.

Sample Code:

MyActivity.java

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

MyService.java

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

MyAsyncTask.java

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

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

Nenhuma solução correta

Outras dicas

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;
      //xxx

  }

  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 ();
      myTask.active = activity;
      return myTask;
     }

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

  protected void onPostExecute(String result) { 

    //check result sucess cond and call
    active.updateActivity(result)

    }
  }

Activity class

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         myService.getInstance(this).startTask();
    }

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

You can send result from service(MyAsyncTask.java and MyService.java) 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[myactivity.java]");
intent.putExtra("RESULT", result)
sendBroadcast(intent);

and your myactivity class receive sending broadcast like this:

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    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:

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(receiver, new IntentFilter("your service class"));
}     

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top