没有互联网消息的WebView片段与拉做刷新

编程入门 行业动态 更新时间:2024-10-26 22:26:06
本文介绍了没有互联网消息的WebView片段与拉做刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这里搜索了有关无互联网消息"的网页视图,并尝试了一些代码,但没有成功.我在片段中有一个WebView,我想检查设备是否具有Internet.如果是,则将加载Webview.如果否,则将加载另一个片段,并且该新片段将在没有互联网消息的情况下使布局膨胀,并且该布局将具有刷新按钮(或下拉菜单刷新).那么,我该如何实施网络检查和刷新?

I searched here about "no internet message" for a webview, and i tried some codes, but no sucess. I have one WebView in fragment and i want to check if the device have internet or no. If yes, the webview will load. If no, another fragment will be load, and this new fragment will inflate a layout with the no internet message, and this layout will have a button to refresh (or a pull down to refresh). So, how can i implement the network check and refresh ?

主要活动

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(null == savedInstanceState) { // set you initial fragment object FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content, InicioPagina.newInstance()); transactionmit(); }

WebView片段

public class InicioPagina extends Fragment { private ProgressBar prg; SwipeRefreshLayout mySwipeRefreshLayout; public WebView myWebView; public static InicioPagina newInstance() { InicioPagina fragment = new InicioPagina(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.iniciopagina, container, false); prg = (ProgressBar)rootView.findViewById(R.id.progressBar2); mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipelayout); myWebView = (WebView) rootView.findViewById(R.id.inicio_pagina); myWebView.loadUrl("example.br/"); //*Ativar JavaScript WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); //*Forçar links para abrir no WebView ao invés do navegador myWebView.setWebViewClient(new WebViewClient()); myWebView.setVerticalScrollBarEnabled(false); mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { myWebView.reload(); mySwipeRefreshLayout.setRefreshing(false); } }); myWebView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN) { WebView webView = (WebView) v; switch(keyCode) { case KeyEvent.KEYCODE_BACK: if(webView.canGoBack()) { webView.goBack(); return true; } break; } } return false; } }); return rootView; } public class WebViewClient extends android.webkit.WebViewClient{ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { prg.setVisibility(View.VISIBLE); super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { prg.setVisibility(View.GONE); super.onPageFinished(view, url); } }

内容主xml

<android.support.constraint.ConstraintLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" xmlns:app="schemas.android/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main" tools:context="br.example.example.MainActivity"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </RelativeLayout>

无互联网布局

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Sem acesso a internet" android:textAlignment="center" android:textSize="30sp" />

推荐答案

您可以通过访问网络状态来检查Internet可用性:

You can check internet availability by access to network state :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

,您可以通过以下方法检查互联网:

and you can check the internet by depend on this method :

private boolean haveNetworkConnection() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; }

ex:

if (!haveNetworkConnection()) { //inflate your fragment, THERE IS NO INTERNET!! }else { //you can reload or what ever you want now. }

更多推荐

没有互联网消息的WebView片段与拉做刷新

本文发布于:2023-11-27 16:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   片段   消息   WebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!