- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我实现了一个 webview 来显示包含一些有用信息的网页。现在,最初我检查了 Netwotk 是否可用。如果可用,它将连接到网页。如果没有,它会显示一个警报对话框,告诉您设备上没有互联网连接。之后它将重定向到设备的设置选项。我可以从此选项切换到 wifi。但问题是打开wifi后,当我回到工作页面时,它没有自动更新。我必须转到我的选项菜单页面,然后单击按钮,然后它会被更新。如何在打开互联网后像谷歌浏览器一样更新页面。这是我的代码
我的编辑代码
public class JobPage extends AppCompatActivity {
private WebView webView;
public static final String WIFI = "Wi-Fi";
public static final String ANY = "Any";
private static final String URL = "https://app.com";
private static boolean wifiConnected = false;
private static boolean mobileConnected = false;
public static boolean refreshDisplay = true;
public static String sPref = null;
// The BroadcastReceiver that tracks network connectivity changes.
private NetworkReceiver receiver = new NetworkReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.job_layout);
webView=(WebView)findViewById(R.id.webView);
// Registers BroadcastReceiver to track network connection changes.
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkReceiver();
this.registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregisters BroadcastReceiver when app is destroyed.
if (receiver != null) {
this.unregisterReceiver(receiver);
}
}
// Refreshes the display if the network connection and the
// pref settings allow it.
// Checks the network connection and sets the wifiConnected and mobileConnected
// variables accordingly.
@Override
public void onStart () {
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Wi-Fi");
updateConnectedFlags();
if(refreshDisplay){
loadPage();
}
}
public void updateConnectedFlags() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) {
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
} else {
wifiConnected = false;
mobileConnected = false;
}
}
public void loadPage() {
if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected))
|| ((sPref.equals(WIFI)) && (wifiConnected))) {
webView.loadUrl(URL);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
refreshDisplay=true;
} else {
errorDialog();
}
}
public void errorDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setMessage("No internet connection on your device. Would you like to enable it?")
.setTitle("No Internet Connection")
.setCancelable(false)
.setPositiveButton("Enable Internet",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(dialogIntent);
}
});
builder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conn = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
// Checks the user prefs and the network connection. Based on the result, decides whether
// to refresh the display or keep the current display.
// If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection.
if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// If device has its Wi-Fi connection, sets refreshDisplay
// to true. This causes the display to be refreshed when the user
// returns to the app.
Toast.makeText(context, "Wi-fi is connected", Toast.LENGTH_SHORT).show();
JobPage.refreshDisplay = true;
// If the setting is ANY network and there is a network connection
// (which by process of elimination would be mobile), sets refreshDisplay to true.
} else if (ANY.equals(sPref) && networkInfo != null) {
JobPage.refreshDisplay = true;
// Otherwise, the app can't download content--either because there is no network
// connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there
// is no Wi-Fi connection.
// Sets refreshDisplay to false.
} else {
JobPage.refreshDisplay = false;
Toast.makeText(context, "Lost internet connection", Toast.LENGTH_SHORT).show();
}
}
}
最佳答案
您需要添加一个 BroadcastReceiver 来处理 CONNECTIVITY_CHANGE
.这将允许您的应用在网络状态发生变化时收到通知,例如没有连接互联网。
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
boolean isOnline = isOnline( context );
// Fire an event with the new status of the network.
Bus bus = new Bus(ThreadEnforcer.MAIN);
bus.post( new NetworkEvent( isOnline ) );
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<receiver
android:name="NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
public class NetworkEvent {
private boolean isOnline;
public NetworkEvent( boolean isOnline ) {
this.isOnline = isOnline;
}
}
Subscribe
到事件。
public class JobPage extends AppCompatActivity {
@Override
protected void onCreated( Bundle savedInstanceState ) {
...
Bus bus = new Bus(ThreadEnforcer.MAIN);
bus.register(this);
...
}
...
@Subscribe
public void onNetworkChange( NetworkEvent event ) {
if( event.isOnline ) {
// Do refresh.
}
}
}
关于android - 连接到互联网后如何自动刷新网页 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45710559/
最近两年,互联网+的概念可谓十分火爆。所谓“互联网+”,其实质就是把互联网大平台和各行各业进行有机结合,建立一个新的商业生态,对于传统企业来说,互联网+的第一步就是有一个企业网站,将自己推广出去
我的大学分两步运行他们的 wifi 身份验证。首先您连接到 wifi 网络,然后当您打开网络浏览器时,您必须输入一些关于您自己的附加信息才能访问互联网。 1)大学系统有一些方法可以检测设备之前是否已经
有没有办法监控每个应用程序使用了多少数据(互联网)? 如果我无法监控应用程序,是否有办法获取所有互联网请求信息?(获取传递的数据、url 等) (我想在为 iPhone 开发的应用程序中使用此信息)
我想从我的 MTI RFID ME Gen2 Internet 中读出一些标签: http://www.mti.com.tw/rfidme/ 这是一个 USB Dongle,现在我想用 c# 在 VS
我正在开发一个应用程序。其中,我想在一段时间内禁止使用互联网,尤其是在他有作业的情况下。如何禁用互联网? 最佳答案 要以编程方式启用/禁用数据连接,请检查此 post 要禁用/启用 wi-fi,请参阅
在我的应用程序中,我只需要知道设备是否连接到 wifi 网络。我认为此功能适用于模拟器但不适用于真实设备。 public static boolean wifiInternet(Context c)
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 12 年前。 Improve th
设想一种情况,我的 PC 有两个网卡,一个连接到互联网,另一个连接到本地网络,我如何用 C# 检测连接到互联网的 IP? 最佳答案 试试这个: static IPAddress getInternet
我的 firebase 应用程序在我的 android 设备上运行完美,无需添加 permission.INTERNET 标志。我直接从 gradle 安装了 Firebase。我知道通过 andro
最近我开始考虑一种解决方案,通过 Internet 向我的订阅客户发布消息。我们的系统是用 C# 开发的。 我们尝试使用 Redis,它在速度和准确性方面非常好,但在安全性方面非常糟糕,每个人都可以订
国内公司普遍不注重基础设施建设,这也是可以理解的。吃饭都吃不饱,就别提什么荤素搭配,两菜一汤了。但也不能全说是这样,还是有很多公司投入大量的人力物力去做好公司的基建,比如很多阿里和美团的小伙伴对公司
我有几个 Azure Functions(高级计划),它们可以执行一些操作并将结果加载到存储 blob。与存储帐户的连接受到 v-net 的限制,因此无法公开访问存储帐户,但是,我检查并发现我的 (H
我正在我的家庭有线网络上解析 ICMPv6 数据报,但在特定的 RFC 中找不到对位排序约定的明确提及。 多字节字段是网络顺序,但是一个字节内的位呢? 机器是字节可寻址的,但网络硬件将位序列化。在图表
我如何在笔记本电脑上配置以太网或 wifi,我遵循了这个 tutorial我的以太网正常启动,但我仍然无法从 ping 8.8.8.8 dns 收到任何数据包,我的笔记本电脑上有以太网和 wifi .
我在 Genymotion 模拟器上运行的应用程序需要互联网。网上好像不能用Genymotion模拟器。我试图通过打开浏览器来确认这一点,这就是我得到的: 我在笔记本电脑上运行 Windows 7 并
我目前正在学习 docker 和 kubernetes。我遇到的问题之一是将我的 nginx pod 暴露在公共(public)互联网上。我想从我的网络浏览器访问我的 serverIP 并查看 ngi
是否可以为 ImageSpan 指定 Internet url 并使用 TextView 显示它?我已经尝试了很多版本 String mockContent = ""; myTextView.setT
例子。 我在 Google 主页上做了一些 CSS 更改。我想保存 CSS(某处)并在每次加载页面时都具有相同的外观。 欢迎使用任何浏览器插件或任何其他方法。 最佳答案 如果使用谷歌浏览器,有一个名为
我正在构建一个旨在加载到 Wii Internet Channel(本质上是 Opera 9.3)的 Web 应用程序。 它支持 SVG,但它们似乎无法缩放。无论宽度和高度设置为多少,图形始终以其“最
我安装了 python 2.7.5,它工作正常。 然后我安装scrapy(我认为,它在内部使用了twisted)。我的爬虫蜘蛛也工作正常。 我安装了扭曲: sudo apt-get install p
我是一名优秀的程序员,十分优秀!