I have a toast in a slave thread which needs t开发者_如何学Pythono tell a user wen a connection is established. To do this I know I need to use Async to make the toast happen, but I'm not sure where or how to implements the extended async. If I understand it, I think I can just create a MyAsync with the and just onProgressUpdate() the toast?
@Override
public void onProgressUpdate(String... args) {
Toast.makeText(context, args, Toast.LENGTH_SHORT).show();
}
Thanks for your time ~Aedon
Yep, you should be able to just extend the ASyncTask and change the template variables to what you need. The Toast class is a static class so it can be called from any thread without worrying about conflicts.
I don't see any issues with your code above except you wouldn't want to be calling new Toast messages very often since they stack. So if you were to continuous call the .show() function it would stack them and continue to show new Toast messages every LENGTH_SHORT interval until it caught up.
As for an example of an ASyncTask, here you go:
private class MyAsync extends AsyncTask<<What to pass in to doInBackground>, <What to pass in to onProgressUpdate>, <What type onPostExecute receives>> {
protected T (result type to onPostExecute) doInBackground(T... urls) {
//Do big calculations in here
}
protected void onProgressUpdate(T... progress) {
//Update
}
protected void onPostExecute(T result) {
//Done
}
}
精彩评论