using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Java.Lang; using Android.App; using System.Collections.Generic; using Android.Util; using Java.Util; using System.Linq; namespace SampleApplication { public class ToastCompat { static Queue queueViews = new Queue(); static int toastduration = 500; static MsgView msgView; //static Handler handler = null; static bool isShowingToast = false; static System.Threading.Thread gthreadToast = null; MsgView msg; static bool gblnToastThreadRunning = false; private ToastCompat() { } public static ToastCompat MakeText(Context context, string text, ToastLength duration) { if (duration == ToastLength.Short) { toastduration = 2000; } else { toastduration = 3500; } msgView = new MsgView((Activity)context, text, toastduration); msgView.Duration = duration; queueViews.Enqueue(msgView); return new ToastCompat(); } public void Show() { try { if (gthreadToast == null) { gthreadToast = new System.Threading.Thread(new System.Threading.ThreadStart(createToastView)); gthreadToast.IsBackground = true; gthreadToast.Priority = System.Threading.ThreadPriority.Lowest; gthreadToast.Start(); } } catch { if (gthreadToast != null) { if (!gthreadToast.IsAlive) { try { gthreadToast.Interrupt(); } catch (System.Exception) { } gthreadToast = null; gthreadToast = new System.Threading.Thread(new System.Threading.ThreadStart(createToastView)); gthreadToast.IsBackground = true; gthreadToast.Priority = System.Threading.ThreadPriority.Lowest; gthreadToast.Start(); } } } } public void createToastView() { try { gblnToastThreadRunning = true; while (gblnToastThreadRunning) { if (!isShowingToast) { if (queueViews.Count > 0) { Activity activity = GetCurrentActivity(); if (activity != null) { activity.RunOnUiThread(() => { if (!isShowingToast && queueViews.Count > 0 && !activity.IsFinishing) { isShowingToast = true; msg = queueViews.Dequeue(); Toast.MakeText(activity, msg.message, msgView.Duration).Show(); isShowingToast = false; } if (queueViews.Count == 0) { stopThread(); } }); } } } } } catch { isShowingToast = false; } } void HideToast() { try { msg.activity.RunOnUiThread(() => { if (msg.WindowManager != null) { if (msg.MView != null) { msg.activity.GetSystemService(Context.WindowService).JavaCast().RemoveViewImmediate(msg.MView); } } }); } catch { } finally { isShowingToast = false; } } void stopThread() { try { gblnToastThreadRunning = false; if (gthreadToast != null) { if (gthreadToast.ThreadState == System.Threading.ThreadState.Running) gthreadToast.Abort(); gthreadToast = null; } } catch { } } bool IsActivityRunning(Activity activity) { List tasks = null; bool isActivityRunning = false; new Runnable(() => { ActivityManager activityManager = (ActivityManager)activity.GetSystemService(Context.ActivityService); tasks = activityManager.AppTasks.ToList(); foreach (ActivityManager.AppTask task in tasks) { if (activity.TaskId == task.TaskInfo.Id) isActivityRunning = true; } }).Run(); return isActivityRunning; } public class MsgView { public string message = ""; public Activity activity; public int duration = 500; public MsgView(Activity activity, string msg, int Duration) { duration = Duration; message = msg; this.activity = activity; MView = configureToastView(activity, msg); } public LinearLayout MView { get; set; } public IWindowManager WindowManager { get; set; } public ToastLength Duration { get; set; } public LinearLayout configureToastView(Activity activity, string msg) { try { LinearLayout toastLayout = new LinearLayout(activity); toastLayout.Background = Application.Context.GetDrawable(Android.Resource.Drawable.ToastFrame); toastLayout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent); TextView tv = new TextView(activity); tv.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent); tv.SetText(msg, TextView.BufferType.Normal); tv.SetTextColor(Android.Graphics.Color.White); tv.SetTextSize(ComplexUnitType.Sp, 14f); var typeface = Android.Graphics.Typeface.Create("sans-serif", Android.Graphics.TypefaceStyle.Normal); tv.Typeface = typeface; toastLayout.AddView(tv); return toastLayout; } catch { return null; } } } /// /// Gets the current activity of the application(must run on the UI thread for older Android versions). /// /// The current activity. /// App. public static Activity GetCurrentActivity() { try { Activity activity = null; List objects = null; var activityThreadClass = Class.ForName("android.app.ActivityThread"); var activityThread = activityThreadClass.GetMethod("currentActivityThread").Invoke(null); var activityFields = activityThreadClass.GetDeclaredField("mActivities"); activityFields.Accessible = true; var obj = activityFields.Get(activityThread); if (obj is JavaDictionary) { var activities = (JavaDictionary)obj; objects = new List(activities.Values.Cast().ToList()); } else if (obj is ArrayMap) { var activities = (ArrayMap)obj; objects = new List(activities.Values().Cast().ToList()); } else if (obj is IMap) { var activities = (IMap)activityFields.Get(activityThread); objects = new List(activities.Values().Cast().ToList()); } if (objects != null && objects.Any()) { foreach (var activityRecord in objects) { var activityRecordClass = activityRecord.Class; var pausedField = activityRecordClass.GetDeclaredField("paused"); pausedField.Accessible = true; if (!pausedField.GetBoolean(activityRecord)) { var activityField = activityRecordClass.GetDeclaredField("activity"); activityField.Accessible = true; activity = (Activity)activityField.Get(activityRecord); break; } } } return activity; } catch { return null; } } } }