代号一-当“未检测到连通性”时为ToastBar。 [英] Codename One - ToastBar when "No connectivity detected"

查看:76
本文介绍了代号一-当“未检测到连通性”时为ToastBar。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决Internet连接不可用(在移动设备上很常见)的正确方法是什么?

What is a correct approach to deal with Internet connection unavailability (that is common on mobile devices)?

也许这是一个大问题(也许不是),但是我没有找到任何Codename One教程/文章/视频或API来处理不稳定的Internet连接(这是手机上的正常现象),没有错误或意外行为。 我找到了一个应用程序(Protonmail),该应用程序具有要复制到我的应用程序中的功能。请查看以下两个屏幕截图:第一个是在Internet连接可用时拍摄的,第二个是在Internet连接不可用时拍摄的。

Maybe it's a big question (or maybe not), however I didn't find any Codename One tutorial / article / video or API to deal with an unstable Internet connection (that is the normality on mobile) without errors or unexpected behaviors. I've found an app (Protonmail) that has a functionality that I would like to replicate in my apps. Please look at the following two screenshot: the first one is taken when the Internet connection is available, the second one when it's not available.

我注意到ToastBar不仅会在明确禁用Internet时出现,还会在服务器无法访问时出现。当显示未检测到连接消息时,该应用程序暂停Internet活动,因此不会产生任何错误(并且用户写入的消息不会丢失)。当应用服务器恢复到可访问状态时,ToastBar会自动消失(无需按 RETRY按钮)。

I noted that the ToastBar appears not only when Internet is explicitly disabled, but every time that the server is not reachable. When the "No connectivity detected" message is shown, the app pauses the Internet activity, so no errors are generated (and the messages written by the user are not lost). When the app server returns to be reachable, the ToastBar disappears automatically (without pressing the "RETRY" button).

在代号中实现类似功能的正确方法是什么一个,以一种尽可能独立于特定应用程序的方式吗?是否可以挂起Codename One应用程序的Internet活动,然后将其还原?

What is a correct way to implement a similar functionality in Codename One, in a way that is as most as possible independent from the specific app? Is it possible to suspend the Internet activity of a Codename One app and then restore it?

我在想像这样的东西:


  • 该应用尝试执行每隔几秒钟向服务器发送一个简单请求(例如ping),实现performBackgroundFetch方法;

  • 如果在固定时间内(即三秒钟)没有响应,则表明Internet活动为暂停并显示ToastBar;

  • 如果有响应,则恢复Internet活动,重新开始暂停或失败的下载,ToastBar消失;

  • 理想情况下,所有这些都应与BrowserComponent一起使用。

  • the app tries to do a simple request (like a ping) to a server every few seconds, implementing the performBackgroundFetch method;
  • if there is no response in a fixed time (i.e. three seconds), the Internet activity is suspended and the ToastBar is shown;
  • if there is response, the Internet activity is restored, the paused or failed downloads are restarted and the ToastBar disappears;
  • ideally all of this should works also with a BrowserComponent.

推荐答案

您可以检测到一个 NetworkManager 类中的网络错误,方法是:

You can detect a networking error in the NetworkManager class by using:

NetworkManager.getInstance().addErrorListener(e -> {
     // prevents the error from propagating into the ConnectionRequest class
     e.consume();

     ToastBar.showMessage("Connectivity error, retry?", FontImage.MATERIAL_ERROR, 
         ee -> ee.getConnectionRequest().retry());
});

如此处所述: https://www.codenameone.com/manual/files-storage-networking.html

吐司条形码仅以类似的方式提示,并提供对连接请求的重试。注意,这是通用的全局方法。

The toast bar code just prompts in a similar way and offers a retry on the connection request. Notice that this is the generic global approach.

这不适用于诸如浏览器组件之类的东西,这些组件在没有 us的情况下自行连接。在那里,您将需要处理JavaScript方面的错误。

This will not work for things like browser component which connects on its own without "us". In there you will need to handle errors in the JavaScript side.

这篇关于代号一-当“未检测到连通性”时为ToastBar。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆