I’m trying to write a test app to zip and unzip a file. I found a open source library for doing the file compression/uncompression but as you can guess that does take sometime so I can’t run it in the main ui so I thought I’d use AsyncTask to do the heavy lifting on a background thread, and then just throw up started, and a finished message and everything would be fine. but I’m having two big problems here. when I hit the button, it does go and unzip the files but it it appears to do it in the main ui not in a background thread as the app will lock up and I do get the anr screen, plus I’m watching for any new threads do appear using the ddms view and I don’t see any. if I tell it I want to wait it out it will unzip the files. but of course I do not want the program to appear to freeze or anr on me the other problem is I have it set to update the button text both right after it’s clicked on, and then again when the aysc task finishes but yet I don’t see either change happen, i did change the texts so that they where all different so I could tell. also it’s supposed to set a text field in the status message. I don’t think the onPostExecute is ever being called. can someone tell me what is going on here?
package net.pawworks.sandbox.svc;
import java.io.File;
import net.pawworks.utils.mainlib.filetools;
import net.pawworks.utils.mainlib.zipper;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestserviceActivity extends Activity {
/** Called when the activity is first created. */
String readpath= filetools.sdcardpath()+"/ziptestin/";
TextView readstatus;
Button readbut;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ziptest);
File setup = new File(filetools.sdcardpath()+"/ziptestout");
setup.mkdir();
setup = new File(filetools.sdcardpath()+"/ziptestin/");
setup.mkdir();
readstatus = (TextView)findViewById(R.id.readstatus);
readbut = (Button)findViewById(R.id.readit);
readbut.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
readbut.setText("Loading.....");
String status="";
// TODO Auto-generated method stub
File setup = new File(readpath+"jpgs.zip");
if(setup.canRead())
{
status="File Readable\n";
new unpackit().doInBackground(new String[]{"jpgs.zip",status} );
readbut.setText("Read!");
}
else {status = "File not Readable:"+setup.toString();readstatus.setText(status);readbut.setText("Read.");}
}
});
}
private class unpackit extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
Log.e("asynctask", "1:"+params[0]+"2:"+params[1]);
zipper d = new zipper(readpath+"jpgs.zip",filetools.sdcardpath()+"/ziptestout/");
d.unzip();
return params[1]+"\n"+d.status();
}
@Override
protected void onPostExecute(String result)
{
readstatus.setText(result);
readbut.setText("Read?");
}
}
}
You want to be calling
rather than doInBackground(args). It’s analogous to calling start() on a thread, rather than run() – in this case, your UI thread is just executing the code in doInBackground(), but it’s not invoking anything else.
For more information see the documentation and sample code here.