Android: get result from onPostexecute AsyncTask in service -


I am using asyncTask to download a file. AsynkTask in a service. I would like to download it, doing some years with the data received from OnPostexecute of AsynkTask. I can see the data from logs, onPostexecute, but I could not get it in the service. I saw many responses but did not work for me, I hope someone will provide me the solution. Thank you in advance.

I started the service with main activity.

** Edit 3 * Now there is no exception, I've added the reference to the new DownloadFileFromURL (this) .execute (file_url);

This code is now working.

After editing, I am performing an executable I am using this code: This service:

  Public Class DownloadService service applies OnTaskCompleted {// file URL to private string file_url = "http://192.168.1.150: 8080 / TestAndroid / DownloadServlet / Desert.jpg"; Public Ink on Start Commands (Intent Intent, Int Flag, Interest StartID) {int rssi1 = intent.getIntExtra ("key1", 0); New DownloadFileFromURL (this) .execute (file_url); Return START_STICKY; } // Edit DownloadFileFromURL function = new DownloadFileFromURL (this); @Ivarried Public Ibner on Bint (Intent Intent) {// TODO Auto-generated method stub returns empty; } @ Override Task Completed on Public Zero (string result) {// Toast to complete your work. GetBaseContext, "service from post" + result, toast LNNHT_Leng) Show (); }}   

and this enhances my DownloadFileFromURL class

  class DownloadFileFromURL AsyncTask & lt; String, string, string & gt; {// edit private joystick Complex Listener; Public DownloadFileFromURL (Ontask Completed Listener) {this.listener = Listener; } @ Override Protected Zero at PreExecute () {super.onPreExecute (); } / ** * File downloads in background threads * * / Protected string doInBackground (string ... f_url) {int count; Try {// Download} Hold (exception E) {} reslut returns; } Protected zero progress progress (string ... progress) {} / ** * Post completion of background work * * ** / @ override protected zero at postexec (string wear) {listenener.onTaskCompleted (reslut); Log.i ("reslut", "************" + reslut); // I want to send this data for service}}   

Interface:

  public interface ontask full (zero) onTaskCompleted (string value);   

}

! Create an interface

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

2. Complete the onscreen in your service

  The Public Class DownloadService service applies OnTaskCompleted {// file string file = url = "http Download To url: //192.168.1.150: 8080 / TestAndroid / DownloadServlet / Desert.jpg "; Public Ink on Start Commands (Intent Intent, Int Flag, Interest StartID) {int rssi1 = intent.getIntExtra ("key1", 0); New DownloadFileFromURL () Execute (file_url); Return START_STICKY; @Ivarried Public Ibner on Bint (Intent Intent) {// TODO Auto-generated method stub returns empty; Void onTaskCompleted (boolean isSuccess) {// Your stuff upon completion of}}   

3 PostExecute you must set the listener true

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

It is only you to do this, it probably will not work, you have to make the necessary changes

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -