Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android不离开doInBackgroundThread而不离开活动?_Android_Multithreading_User Interface_Background - Fatal编程技术网

Android不离开doInBackgroundThread而不离开活动?

Android不离开doInBackgroundThread而不离开活动?,android,multithreading,user-interface,background,Android,Multithreading,User Interface,Background,这一切都是因为我想在后台线程中显示一条toast消息。Toast消息在尝试加载应用程序时会崩溃,而不告诉它们在UI线程上加载。现在我意识到我想显示Toast消息并关闭后台线程,这是一种错误处理,让用户知道错误 后台线程中所需的条件将用户带到新活动并完成当前活动。这很有效 现在我注意到,我不知道还有什么方法可以结束背景线程并在同一活动中显示祝酒词 如何从doInBackground结束后台线程?您需要为任务执行cancel方法。然后,当它被取消而不是运行onPostExecute时,它将调用onC

这一切都是因为我想在后台线程中显示一条toast消息。Toast消息在尝试加载应用程序时会崩溃,而不告诉它们在UI线程上加载。现在我意识到我想显示Toast消息并关闭后台线程,这是一种错误处理,让用户知道错误

后台线程中所需的条件将用户带到新活动并完成当前活动。这很有效

现在我注意到,我不知道还有什么方法可以结束背景线程并在同一活动中显示祝酒词


如何从doInBackground结束后台线程?

您需要为任务执行cancel方法。然后,当它被取消而不是运行onPostExecute时,它将调用onCancelled。把你的祝酒词放在那里。

RD如果我可以建议一种处理后台线程中错误的扭曲方法的话。考虑在TestCcatch中包装后台线程中的调用并返回有状态的对象。因此,如果希望后台线程返回字符串,请创建BoolString类

//a utility class to signal success or failure, return an error message, and return a useful String value
//see Try Out in C#
public final class BoolString {
 public final boolean success;
 public final String err;
 public final String value;

 public BoolString(boolean success, String err, String value){
     this.success= success;
     this.err= err;
     this.value= value;
 }
}
用法:

public BoolString tryEncrypt(String inString, String password) {
    try {
        String value= encrypt(inString, password);
        return new BoolString(true,"",value);
    }
    catch (GeneralSecurityException e){
        return new BoolString(false,e.getMessage(),"");
    }
}

    protected void onPostExecute(BoolString result){          
        progress.dismiss();
        if (result.success){
                result.value;
        }
        else {
              result.err;
        }
    }

谢谢,cancel方法会关闭对话框,但我无法执行OnCancell!在调用后台线程的活动中,我有一个_initTask.canceltrue,如果活动停止,它将停止后台线程。然后在我的doInBackground方法中,我在尝试返回之前检查this.isCancelled。当doInBackground检测到isCancelled时,它会自动执行onCancelled方法,而不是onPostExecuteMethod。