Android离开doInBackgroundThread而不离开活动?(Android get out of doInBackgroundThread without leaving the act

编程入门 行业动态 更新时间:2024-10-28 06:32:07
Android离开doInBackgroundThread而不离开活动?(Android get out of doInBackgroundThread without leaving the activity?)

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

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

现在我注意到我不知道另一种方法来结束backgroundthread并在同一个活动上显示toast消息?

如何从doInBackground结束后台线程?

this all started because I wanted to display a toast message in a background thread. (Toast messages crash the app when trying to load them without telling them to load on UI thread) Now I realize I want to display a toast message AND close the background thread, this is a kind of error handling and letting the user know of the error.

The desired condition in the background thread takes the user to a new activity and finishes the current one. That works.

Now I noticed that I don't know of another way to end the backgroundthread and show the toast message on the same activity?

How do I end the background thread from doInBackground?

最满意答案

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

You need to execute the cancel() method for your task. Then when it's cancelled rather than running the onPostExecute() it will call onCancelled(). Put your Toast message in there.

更多推荐

本文发布于:2023-08-08 00:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   doInBackgroundThread   Android   leaving   activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!