android asynctask 总是这么慢吗

发布于 2024-11-19 18:20:31 字数 1318 浏览 0 评论 0原文

我正在使用 asynctask 来运行客户端协议。异步任务工作正常,但是需要(非常明显的)几秒钟才能开始运行。

我已经完成了一些调试,调用执行的按钮保持突出显示几秒钟,然后 onPreExecute() 触发,并且我正在运行的进程栏启动。

所以我的问题很简单:Asynctask 启动总是这么慢吗?或者这里是否有可能出现某种问题?

这是有问题的按钮(和 onClickListener)。这段代码可以在 onCreate(Bundle SavedInstanceState) 中找到:

mSave = (Button)findViewById(R.id.btnSave);
mSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Toast.makeText(getBaseContext(),
        "marker", Toast.LENGTH_LONG).show();
    if (portrait != null && !mComments.getText().toString().equals("")) {
        for (int i = 0; i < 6; i++) {
            checked[i] = checkBoxes[i].isChecked();
            checkBoxFields[i] = checkBoxes[i].getText().toString();
        }
        new ClientProtocol().execute();
     }
    }
});

这里是 onPreExecute(),尽管我很确定暂停是在此之前的某个地方:

@Override
protected void onPreExecute() {
    mProgress = 0;
    mLoad.setVisibility(View.VISIBLE);
    mSave.setClickable(false);

    mFinal = "";

    mClientThoughts = mComments.getText().toString();
    mCheckBoxes = checkBoxFields;
    mChecked = checked;

    mBaos = new ByteArrayOutputStream();
    portrait.compress(Bitmap.CompressFormat.PNG, 100, mBaos);
    mClientImage = mBaos.toByteArray();
}

I'm using an asynctask to run a client protocol. The asynctask works fine, however it takes a (very noticable) few seconds to start running.

I've done some debugging and the button that calls the execute remains highlighted for several seconds, then onPreExecute() fires, and the process bar that I have running starts.

So my question is simply: is Asynctask always this slow to start or is there a chance of some sort of problem here?

Here's the button (and the onClickListener) in question. This code is found in onCreate(Bundle SavedInstanceState):

mSave = (Button)findViewById(R.id.btnSave);
mSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Toast.makeText(getBaseContext(),
        "marker", Toast.LENGTH_LONG).show();
    if (portrait != null && !mComments.getText().toString().equals("")) {
        for (int i = 0; i < 6; i++) {
            checked[i] = checkBoxes[i].isChecked();
            checkBoxFields[i] = checkBoxes[i].getText().toString();
        }
        new ClientProtocol().execute();
     }
    }
});

Here's onPreExecute(), although I'm pretty sure the pause is somewhere before this:

@Override
protected void onPreExecute() {
    mProgress = 0;
    mLoad.setVisibility(View.VISIBLE);
    mSave.setClickable(false);

    mFinal = "";

    mClientThoughts = mComments.getText().toString();
    mCheckBoxes = checkBoxFields;
    mChecked = checked;

    mBaos = new ByteArrayOutputStream();
    portrait.compress(Bitmap.CompressFormat.PNG, 100, mBaos);
    mClientImage = mBaos.toByteArray();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-11-26 18:20:31

从你的描述来看,延迟似乎是在你的 onPreExecute() 触发之前?是否有什么东西延迟了您的按钮处理(也许是处理程序?)导致了这种情况?

From your description seems like the delay is even before your onPreExecute() fires? Is there perhaps something delaying your button processing (maybe handler?) causing this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文