Created
December 18, 2015 20:09
-
-
Save joeynebula/524fecdfc014ee412185 to your computer and use it in GitHub Desktop.
Helper class to send toasts from background thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.os.Handler; | |
| import android.widget.Toast; | |
| /* | |
| Taken from an answer in the following thread | |
| http://stackoverflow.com/questions/7378936/how-to-show-toast-message-from-background-thread | |
| From user kaolick | |
| * / | |
| /** | |
| * A class for showing a <code>Toast</code> from background processes using a | |
| * <code>Handler</code>. | |
| * | |
| * @author kaolick | |
| */ | |
| public class ToastHandler | |
| { | |
| // General attributes | |
| private Context mContext; | |
| private Handler mHandler; | |
| /** | |
| * Class constructor. | |
| * | |
| * @param _context | |
| * The <code>Context</code> for showing the <code>Toast</code> | |
| */ | |
| public ToastHandler(Context _context) | |
| { | |
| this.mContext = _context; | |
| this.mHandler = new Handler(); | |
| } | |
| /** | |
| * Runs the <code>Runnable</code> in a separate <code>Thread</code>. | |
| * | |
| * @param _runnable | |
| * The <code>Runnable</code> containing the <code>Toast</code> | |
| */ | |
| private void runRunnable(final Runnable _runnable) | |
| { | |
| Thread thread = new Thread() | |
| { | |
| public void run() | |
| { | |
| mHandler.post(_runnable); | |
| } | |
| }; | |
| thread.start(); | |
| thread.interrupt(); | |
| thread = null; | |
| } | |
| /** | |
| * Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in | |
| * background processes. | |
| * | |
| * @param _resID | |
| * The resource id of the string resource to use. Can be | |
| * formatted text. | |
| * @param _duration | |
| * How long to display the message. Only use LENGTH_LONG or | |
| * LENGTH_SHORT from <code>Toast</code>. | |
| */ | |
| public void showToast(final int _resID, final int _duration) | |
| { | |
| final Runnable runnable = new Runnable() | |
| { | |
| @Override | |
| public void run() | |
| { | |
| // Get the text for the given resource ID | |
| String text = mContext.getResources().getString(_resID); | |
| Toast.makeText(mContext, text, _duration).show(); | |
| } | |
| }; | |
| runRunnable(runnable); | |
| } | |
| /** | |
| * Shows a <code>Toast</code> using a <code>Handler</code>. Can be used in | |
| * background processes. | |
| * | |
| * @param _text | |
| * The text to show. Can be formatted text. | |
| * @param _duration | |
| * How long to display the message. Only use LENGTH_LONG or | |
| * LENGTH_SHORT from <code>Toast</code>. | |
| */ | |
| public void showToast(final CharSequence _text, final int _duration) | |
| { | |
| final Runnable runnable = new Runnable() | |
| { | |
| @Override | |
| public void run() | |
| { | |
| Toast.makeText(mContext, _text, _duration).show(); | |
| } | |
| }; | |
| runRunnable(runnable); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment