Java Android异步任务运行多次
在我的应用程序中,有一个按钮可以从数据库中获取输入。当我在短时间内按下多个按钮时,它就会崩溃。 如何使用asynctask避免此错误Java Android异步任务运行多次,java,android,android-asynctask,Java,Android,Android Asynctask,在我的应用程序中,有一个按钮可以从数据库中获取输入。当我在短时间内按下多个按钮时,它就会崩溃。 如何使用asynctask避免此错误 谢谢…您可以创建一个布尔标志,比如说bDiscardButtonAction,并在onPreExecute中将其设置为true,在onPostExecute中将其设置为false,类似于: public class FooTask extends AsyncTask<Foo, Foo, Foo> { private static boolean
谢谢…您可以创建一个布尔标志,比如说bDiscardButtonAction,并在onPreExecute中将其设置为true,在onPostExecute中将其设置为false,类似于:
public class FooTask extends AsyncTask<Foo, Foo, Foo>
{
private static boolean bDiscardButtonAction = false;
private boolean isDiscareded = false;
@Override
public void onPreExecute()
{
if(bDiscardButtonAction)
{
isDiscareded = true;
return;
}
bDiscardButtonAction = true;
}
@Override
public Foo doInBackground(Foo... params)
{
if(isDiscareded) return;
// ...
}
@Override
public void onPostExecute(Void result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
@Override
public void onCancelled(Foo result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
}
您可以创建一个布尔标志,比如bDiscardButtonAction,并在onPreExecute中将其设置为true,在onPostExecute中将其设置为false,类似于:
public class FooTask extends AsyncTask<Foo, Foo, Foo>
{
private static boolean bDiscardButtonAction = false;
private boolean isDiscareded = false;
@Override
public void onPreExecute()
{
if(bDiscardButtonAction)
{
isDiscareded = true;
return;
}
bDiscardButtonAction = true;
}
@Override
public Foo doInBackground(Foo... params)
{
if(isDiscareded) return;
// ...
}
@Override
public void onPostExecute(Void result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
@Override
public void onCancelled(Foo result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
}
我希望这段代码能帮助你
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
new AsynchTaskManualLocation().execute();
});
public class AsynchTaskGetData extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... url) {
//showinf(); this method contains operation of getting data from //database OR ur logic to getdata
return showinf();
}
@Override
protected void onPostExecute(String result) {
//here u get result in resul var and process the result here
}
}
}
我希望这段代码能帮助你
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
new AsynchTaskManualLocation().execute();
});
public class AsynchTaskGetData extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... url) {
//showinf(); this method contains operation of getting data from //database OR ur logic to getdata
return showinf();
}
@Override
protected void onPostExecute(String result) {
//here u get result in resul var and process the result here
}
}
}
在onPreExecute中禁用“显示”按钮,并在PostExecute上重新启用该按钮
在onPreExecute中禁用“显示”按钮,并在PostExecute上重新启用该按钮
请发布您的代码。请发布一些您到目前为止尝试过的代码。代码+日志猫输出请发布您的代码。请发布一些您到目前为止尝试过的代码。代码+日志猫输出