android asynctask 总是这么慢吗
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从你的描述来看,延迟似乎是在你的 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?