我试图在弹出框的按钮单击上取消/结束异步任务。但是,当我单击按钮时,不会调用onCancelled()方法。有人可以帮我吗?下面是我的代码:
public AuthenticationAsync(Context context, final String rid) {
this.rid = rid;
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Information");
alertDialog.setMessage("RaspberryPi Validated");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cancel(true); //Cancelling async task on button click
dialog.dismiss();
}
});
}
@Override
protected Object doInBackground(Object[] params) {
String metricData = String.valueOf(kuraPayload.getMetric("data"));
if (metricData.isEmpty())
subResponse = false;
}
@Override
protected void onCancelled() {
Log.d("Async","cancelled"); //not being called
}
@Override
protected void onPostExecute(Object o) {
if (subResponse) {
asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get(WSConstants.ADD_RPI_WS + MQTTFactory.getRaspberryPiById(), new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Toast.makeText(ActivityContexts.getMainActivityContext(), "RaspberryPi Validated", Toast.LENGTH_LONG).show();
alertDialog.show();
}
}
}
}
好像永远不会进入,onCancelled()
因为您实际上是alertDialog.show()
在打电话onPostExecute()
。既然doInBackground()
已经完成,没有什么可以取消的。
该cancel()
方法应在doInBackground()
方法执行期间调用,这是在后台线程上运行的唯一方法。
请注意,即使cancel()
在执行期间调用doInBackground()
,该方法的执行也不会立即停止。您应该进行定期检查isCancelled()
,如果返回true,则退出该方法。
后doInBackground()
完成,如果cancel()
已经被调用,那么onCancelled()
将被调用,而不是onPostExecute()
。
这是一个基于您的代码的非常简单的测试:
public class AuthenticationAsync extends AsyncTask {
AlertDialog alertDialog;
String rid;
public AuthenticationAsync(Context context, final String rid) {
this.rid = rid;
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Information");
alertDialog.setMessage("RaspberryPi Validated");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cancel(true); //Cancelling async task on button click
dialog.dismiss();
}
});
}
@Override
protected void onPreExecute(){
//for testing, showing the dialog before the background Thread starts
alertDialog.show();
}
@Override
protected Object doInBackground(Object[] params) {
//cycle forever until the user clicks OK
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Check if user clicked OK in the dialog
if (isCancelled()){
//Exit the method if the user dismissed the dialog
return null;
}
}
}
@Override
protected void onCancelled() {
Log.d("Async", "cancelled"); //this is called now
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(Object o) {
Toast.makeText(MainActivity.this, "postExecute", Toast.LENGTH_LONG).show();
}
}
结果:
在执行以下对话框时显示doInBackground()
:
单击确定后,onCancelled()
称为:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句