在Android中取消Asynctask后显示Toast

时间:2014-04-09 17:31:08

标签: android android-asynctask android-toast

当条件不满足时,我需要取消AsyncTask类UploadPhotos,并通过Toast通知用户。我怎么能这样做?

在我的MainActivity中

private void InicializeUploadFiles()
{
    if(!IsNetworkConected())
        ManageConnections();

    if(IsNetworkConected())
    {
        try {
            UploadPhotos upload = new UploadPhotos(this);
            upload.execute();
        } 
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

在我的Asynctask

@Override
protected String doInBackground(Void... args) 
{
  response = "";

  InicializeOwnCloud();

  if(IsExistFiles())
      SendFolders();

  return response;
}

1 个答案:

答案 0 :(得分:1)

覆盖onCancelled() mehod,然后从Toast方法执行onCancelled()消息。

   @Override
    protected void onCancelled() {
        // Show your Toast 
    }