这一切都开始了,因为我想在后台线程中显示一个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.
更多推荐
发布评论