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);
}