AsyncTask not accessible in Service
<p>i tried to figure out whats my problem, the last 3 hours! but without luck. Maybe someone else can help me.</p>

<p>My app does the following:</p>

<p>Activity1 starts Activity2
Acitivity2 starts a Service
The Service uses a AsyncTask to download a file</p>

<p>in the AsyncTask i have a piece of code like this:</p>

<pre><code>while ((status == 0)) {

            byte buffer[];
            if (size - downloaded &gt; MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }

            int read = stream.read(buffer);
            if (read == -1)
                break;

            file.write(buffer, 0, read);
            downloaded += read;

  }
</code></pre>

<p>Everything works like expected. With the <code>status</code> variable i can start and stop my download depending on its value.</p>

<p>BUT, when i close Activity2 and start it again (the service keeps running), i cannot stop the download, what means the variable <code>status</code> is not read correctly. I checked the variable, the value is ok but the Asynctask does not recognize it.</p>

<p>So my question is, how can i get back control over my AsyncTask?</p>

<p><strong>EDIT1:</strong></p>

<p>I made some more tests but this time with a thread, to make sure its not a failure how i handle the AsyncTask. I did it this way:</p>

<p>Activity2 starts the Service (i did not changed any coder here)
The Service creates an <code>Download</code> Object what downloads the file using a Thread.</p>

<p>The Structure looks like this:</p>

<pre><code>in the Service
private Download dl = new Download();

private final DMInterface.Stub mBinder = new DMInterface.Stub() {

    public void downloadFile() throws DeadObjectException {
        try {
            dl.start(url) // This starts a thread and the download

        } catch (IndexOutOfBoundsException e) {
            Log.e(getString(R.string.app_name), e.getMessage());
        }
    }

    public void stop() throws DeadObjectException {
        dl.cancel(); //This stops the download
    }

};
</code></pre>

<p>And again, everything works until i disconnect from the service. So whats the problem? I'm not far away from getting insane. :) Why am i only able to control the Thread when i don't disconnect from the service?</p>

<p><strong>EDIT2:</strong></p>

<p>Here is the code how i start/bind the service to Activity2:
(only they important parts)</p>

<pre><code>public class Activity2 extends ListActivity {

  private DMInterface dmInterface;

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.bindService(new Intent(Activity2.this, DMService.class), mConnection, Context.BIND_AUTO_CREATE);
  }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            dmInterface = DMInterface.Stub.asInterface(service);
            //do some stuff

        }

        public void onServiceDisconnected(ComponentName className) {
            dmInterface = null;
        }
    };
}
</code></pre>

<p>There ar two scenarios. In the first on i get an error in the second one not (but nothing else happens).
When an error is raised depends on, where i initialize the Thread e.g. the Object that starts the Thread.</p>

<p><em>Scenario 1:</em></p>

<p>When i do it like described in EDIT1 i get no error but nothing happens.</p>

<p><em>Scenario 2:</em></p>

<p>In the Service:</p>

<pre><code>private Download dl;

private final DMInterface.Stub mBinder = new DMInterface.Stub() {

    public void downloadFile() throws DeadObjectException {
        try {
            dl = new Download();
            dl.start(url) // This starts a thread and the download

        } catch (IndexOutOfBoundsException e) {
            Log.e(getString(R.string.app_name), e.getMessage());
        }
    }

    public void stop() throws DeadObjectException {
        dl.cancel(); //This stops the download
    }

};
</code></pre>

<p>I get this error: <a href="http://omploader.org/vM241cg/pasta" rel="nofollow">http://omploader.org/vM241cg/pasta</a></p>

<p>When i try to reach other parts of the service (setting a variable or something like that) everything works ok.</p>

<p>Thank you</p>
