tags:

views:

122

answers:

1

Hi there, i tried to figure out whats my problem, the last 3 hours! but without luck. Maybe someone else can help me.

My app does the following:

Activity1 starts Activity2 Acitivity2 starts a Service The Service uses a AsyncTask to download a file

in the AsyncTask i have a piece of code like this:

while ((status == 0)) {

            byte buffer[];
            if (size - downloaded > 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;

  }

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

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

So my question is, how can i get back control over my AsyncTask?

EDIT1:

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:

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

The Structure looks like this:

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
    }

};

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?

EDIT2:

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

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

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.

Scenario 1:

When i do it like described in EDIT1 i get no error but nothing happens.

Scenario 2:

In the Service:

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
    }

};

I get this error: http://omploader.org/vM241cg/pasta

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

Thank you

A: 

A Service lives in its own world, being a Service it is remote from the rest of your program. A Binder can be used to communicate with your service. Defining a aidl interface with a setStatus method allows you to communicate the status to the service.

Rene
Change hyperlink from your local source to web-source. =)
Yeti
Hi Rene, thx for you answer. I know how a Service works (in general) and it works for me too until i close Aktivity2. Then i'm not able to reconnect correctly to the AsyncTask in the service but the service keeps working. The download i started in the service keeps active. If i keep the activity open, i can work with the thread so i think the reconnect to the service(AsyncTask) does not work correctly but i have no clue what exactly goes wrong.
Andy