简体   繁体   English

Xamarin Android 阻止后台应用程序吐司显示

[英]Xamarin Android prevent background app toast display

I have created an xamarin android app incorporating toast popups that display when users lose, then regain internet connection in my app.我创建了一个 xamarin android 应用程序,其中包含在用户丢失时显示的吐司弹出窗口,然后在我的应用程序中重新获得互联网连接。

These toasts appear correctly but when I my app is in the background i still get the toasts displaying when i lose/regain internet connection displaying even over other apps that are in the foreground.这些 Toast 显示正确,但是当我的应用程序在后台时,我仍然会在我失去/恢复互联网连接时显示 Toasts,即使在前台的其他应用程序上也是如此。

I have tried to use a global instance of the toast class then call toast.cancel() on onstop and onpause events, code below.我尝试使用 toast 类的全局实例,然后在 onstop 和 onpause 事件上调用 toast.cancel(),代码如下。 Any ideas?有任何想法吗?

//my global toast class
Toast toast;

//create a toast message and display
if (toast != null) { toast.Cancel(); }
toast = Toast.MakeText(Application.Context, "You are Offline.", ToastLength.Long).show();

protected override void OnStop()
{
      base.OnStop();
      if (toast != null) toast.Cancel();
}

protected override void OnPause()
{
    base.OnPause();
    if (toast != null) toast.Cancel();
}

The below code worked fine for me.下面的代码对我来说很好。 The trick is to keep track of the last Toast that was shown, and to cancel that one correctly.诀窍是跟踪显示的最后一个 Toast,并正确取消那个 Toast。

Refer below link for an exact scenario like yours.有关与您类似的确切场景,请参阅以下链接。

Android cancel Toast when exiting the app and when toast is being shown Android 在退出应用程序和正在显示 Toast 时取消 Toast


Below is just a best practice to use toast.以下只是使用 toast 的最佳实践。

You need to declare a "Toast" var like this at the start of the class.您需要在课程开始时声明一个像这样的“Toast”变量。 Only declaration and no definition.只有声明,没有定义。

Toast toastMessage;

Then in your function, whenever you are using it do it like this:然后在您的函数中,每当您使用它时,请执行以下操作:

if (toastMessage!= null) 
    toastMessage.cancel();
toastMessage= Toast.makeText(context, "Your message", Toast.LENGTH_LONG);
toastMessage.show();

You can first determine whether your app is in the foreground, and then show the prompt according to the situation.你可以先判断你的app是否在前台,然后根据情况显示提示。

method isApplicationInTheBackground方法是isApplicationInTheBackground

private bool isApplicationInTheBackground()
    {
        bool isInBackground;

        RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
        ActivityManager.GetMyMemoryState(myProcess);
        isInBackground = myProcess.Importance != Android.App.Importance.Foreground;

        return isInBackground;
    }

and use like this:并像这样使用:

if(!isApplicationInTheBackground()){
 // show the toast

}else{//don't show the toast

}

Reference from: https://forums.xamarin.com/discussion/134696/how-to-tell-if-android-app-is-in-the-background-or-foreground参考来源: https : //forums.xamarin.com/discussion/134696/how-to-tell-if-android-app-is-in-the-background-or-foreground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM