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