1

我正在尝试编写一个测试应用程序来压缩和解压缩文件。我找到了一个用于进行文件压缩/解压缩的开源库,但是您可以猜到这确实需要一些时间,所以我无法在主 ui 中运行它,所以我想我会使用 AsyncTask 在后台线程上进行繁重的工作,然后就开始吐了,一个完成的消息,一切都会好起来的。但我在这里有两个大问题。当我点击按钮时,它确实会解压缩文件,但它似乎是在主 ui 中而不是在后台线程中执行此操作,因为应用程序将锁定并且我确实得到了 anr 屏幕,而且我正在关注任何使用 ddms 视图确实出现了新线程,但我看不到任何线程。如果我告诉它我想等待它,它将解压缩文件。但当然我不希望程序看起来冻结或在我身上出现另一个问题是我将它设置为在单击它后立即更新按钮文本,然后在 aysc 任务完成时再次更新但我没有'没有看到任何变化发生,我确实更改了文本,以便它们完全不同,所以我可以分辨出来。它还应该在状态消息中设置一个文本字段。我不认为 onPostExecute 被调用过。有人可以告诉我这里发生了什么吗?

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?");
        }
    }
}
4

2 回答 2

7

你想打电话

new unpackit().execute(args);

而不是 doInBackground(args)。这类似于在线程上调用 start() 而不是 run() - 在这种情况下,您的 UI 线程只是执行 doInBackground() 中的代码,但它没有调用任何其他内容。

有关详细信息,请参阅此处的文档和示例代码。

于 2012-04-13T01:07:44.420 回答
3
new unpackit().doInBackground(new String[]{"jpgs.zip",status} );

将上面的行替换为以下::

new unpackit().execute(new String[]{"jpgs.zip",status} );
于 2012-04-13T02:05:03.220 回答