1

I'm using AsyncTask to downoload a file .The asynkTask in in a service .I would like after downloading ,that the service do sometasks with the data returned from the onPostexecute of the asynkTask .I can see the data ,from log ,in the onPostexecute, but i couldn't ,get it in the service. I saw a lot of responses but the did not work for me ,i hope someone will provide me with a solution.Thank you in advance.

I started the service from the main activity.

**EDIT 3 ** No exception anymore , i added the context to new DownloadFileFromURL(this).execute(file_url);

This code is working now.

after editing ,i'm having an execption . This the code i'm using : This the service :

 public class DownloadService extends Service  implements OnTaskCompleted {
        // File url to to download     
        private  String file_url = "http://192.168.1.150:8080/TestAndroid/DownloadServlet/Desert.jpg";
        public int onStartCommand(Intent intent, int flags, int startId) {
            int rssi1 = intent.getIntExtra("key1", 0);
            new DownloadFileFromURL(this).execute(file_url);
            return START_STICKY; }

                    //EDIT
                    DownloadFileFromURL  task = new DownloadFileFromURL(this);
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
@Override
        public void onTaskCompleted(String result){
            //your stuff on complete of task
            Toast.makeText(getBaseContext(), "Service receiving from post"+result, Toast.LENGTH_LONG).show();
        }
    }

And this my DownloadFileFromURL class

 class DownloadFileFromURL extends AsyncTask<String, String, String> {
//Edit
private OnTaskCompleted listener;

    public DownloadFileFromURL (OnTaskCompleted listener){
        this.listener=listener;
    }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
               }
        
        /**
         * Downloading file in background thread
         * */
        protected String doInBackground(String... f_url) {
            int count;
            try { //Download  }


    
                catch (Exception e) {             }
               return reslut;
            }
          
            protected void onProgressUpdate(String... progress) {
           }
        
            /**
             * After completing background task
             * 
             * **/
      

  @Override
         protected void onPostExecute(String reslut) {
listener.onTaskCompleted(reslut);
            Log.i("reslut","**************"+reslut);//i want to send this data to the service
           
               }
        
        }

The Interface :

public interface OnTaskCompleted{
void onTaskCompleted(String values);

}

Community
  • 1
  • 1
Amina
  • 723
  • 8
  • 20
  • 1
    in your service you'll have to override the method written in interface and there you can read the data. – Gautami Apr 22 '14 at 11:15
  • 1
    Service needs to override the onTaskCompleted method – Gautami Apr 22 '14 at 11:20
  • I ovverided it ,but i'm getting an exception on the DownloadFileFromURL.onPostExecute :exacxtly on : listener.onTaskCompleted(reslut); on the onPostexecute – Amina Apr 22 '14 at 11:30
  • I'll edit again my post ,can you take a look to the code ,Thank you .@Gautami – Amina Apr 22 '14 at 11:31
  • 1
    You need to send context in this line new DownloadFileFromURL(this).execute(file_url); I am quite nt sure abt this bt you can try neways. – Gautami Apr 22 '14 at 11:35
  • yes right ,when added this ,there is no exception anymore , but how i would call the method in the service : `public void onTaskCompleted(String result){ //your stuff on complete of task Toast.makeText(getBaseContext(), "Service receiving from post"+result, Toast.LENGTH_LONG).show(); }` – Amina Apr 22 '14 at 11:43
  • 1
    you don't have to call it separately. when you set the listener in postExecute method it will get called automatically as your service implements that listener – Gautami Apr 22 '14 at 11:47

3 Answers3

1

You can create an interface as a listener which will listen to the completion of the task and once complete it will call call method in your service

Gautami
  • 361
  • 1
  • 6
  • Thank you for replying .I saw this methode in a post [link](http://stackoverflow.com/questions/13815807/return-value-from-asynctask-class-onpostexecute-method) ,and i tryed it ,but i did not inderstood how to get finely the result. – Amina Apr 22 '14 at 10:59
  • Please see my edit ,is that correct ? and how to read the data in the service ,like if want to put in a log ?@Gautami – Amina Apr 22 '14 at 11:07
1

To make it easier to use the AsyncTasks I override the methods directly in my Service :

new DownloadFileFromURL() {
    @Override
    protected Object doInBackground(Object... params) {
        // Download 
        return result;
    }
    @Override
    protected void onPostExecute(Object result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // treat result (ui thread will be your service)
            }
        });
    }
}.execute(file_url);
cosmincalistru
  • 1,283
  • 9
  • 20
1

1.Create an interface

public interface OnTaskCompleted{
    void onTaskCompleted(boolean isSuccess);
}

2.Implement OnTaskCompleted in your service

public class DownloadService extends Service implements  OnTaskCompleted  {
    // File url to to download     
    private  String file_url = "http://192.168.1.150:8080/TestAndroid/DownloadServlet/Desert.jpg";
    public int onStartCommand(Intent intent, int flags, int startId) {
        int rssi1 = intent.getIntExtra("key1", 0);
        new DownloadFileFromURL().execute(file_url);
        return START_STICKY;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;

    void onTaskCompleted(boolean isSuccess){
        //your stuff on complete of task
    }
    }

3. In onPostExecute you'll have to set the listener true

protected void onPostExecute(String reslut) {
        listener.onTaskCompleted(values);
           }

This is just how you have to do it , it may not work as it is , you'll have to do the required changes

John
  • 410
  • 1
  • 9
  • 21
Gautami
  • 361
  • 1
  • 6