Java tutorial
//package com.java2s; import android.app.Activity; import android.content.Context; import android.os.Looper; import android.widget.Toast; public class Main { private static final void toast(final boolean isAutoTweet, final Context context, final String text) { if (isAutoTweet == false) { if (!((Activity) context).isFinishing()) { if (currentThreadIsUiThread()) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } else { ((Activity) context).runOnUiThread(new Runnable() { @Override public final void run() { Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } }); } } } } private static final boolean currentThreadIsUiThread() { return Looper.getMainLooper().getThread() == Thread.currentThread(); } }