我想在我的应用中实现以下行为:
如果没有互联网连接,当最后有连接时,将显示一个敬酒警告,网络视图将被加载。
除吐司外,我设法做所有期望的行为,我已经尝试了以下方法:
isOnline();
if (!online) {
Thread y=new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(getBaseContext(), "No hay conectividad a Internet", Toast.LENGTH_LONG).show();
}
});
y.start();
try {
y.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread t=new Thread(new Runnable() {
@Override
public void run() {
while (!online)
{
isOnline();
}
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
还有这个:
isOnline();
if (!online) {
Toast.makeText(getBaseContext(), "No hay conectividad a Internet", Toast.LENGTH_LONG).show();
}
Thread t=new Thread(new Runnable() {
@Override
public void run() {
while (!online)
{
isOnline();
}
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
但是他们两个都没有及时展示敬酒。
我该怎么做才能显示吐司?
让我建议您最简单的方法-只需使用此库:
https://github.com/AggarwalAnkit/InternetAvailabilityChecker
用法非常简单,只需按照教程-https://medium.com/the-sixt-india-blog/check-active-internet-connection-on-android-device-3138ad81932d
因此,您将得到如下内容:
override fun onInternetConnectivityChanged(isConnected: Boolean) {
if (isConnected){
//load webView
} else {
//show Toast here
}
}