我创建了一个WebView布局,用于访问特定网站,但是当手机没有时,编辑或创建自定义“网页不可用”资源会很有用有网络连接或页面超时。我知道这是可能的,因为如果您在手机处于飞行模式时打开应用程序“ Wikidroid ”,则会收到“文章不可用”错误页面而不是标准的Android“网页不可用”错误页面。
我在互联网上搜索高低,并没有提供任何解决此请求的在线资源。任何和所有的帮助非常感谢。提前谢谢。
答案 0 :(得分:20)
要确定设备何时具有网络连接,请请求权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
,然后您可以使用以下代码进行检查。首先将这些变量定义为类变量。
private Context c;
private boolean isConnected = true;
在onCreate()
方法初始化c = this;
然后检查连接性。
ConnectivityManager connectivityManager = (ConnectivityManager)
c.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
if (ni.getState() != NetworkInfo.State.CONNECTED) {
// record the fact that there is not connection
isConnected = false;
}
}
然后拦截WebView
requets,您可以执行以下操作。如果您使用此功能,则可能需要自定义错误消息以包含onReceivedError
方法中可用的一些信息。
final String offlineMessageHtml = "DEFINE THIS";
final String timeoutMessageHtml = "DEFINE THIS";
WebView browser = (WebView) findViewById(R.id.webview);
browser.setNetworkAvailable(isConnected);
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isConnected) {
// return false to let the WebView handle the URL
return false;
} else {
// show the proper "not connected" message
view.loadData(offlineMessageHtml, "text/html", "utf-8");
// return true if the host application wants to leave the current
// WebView and handle the url itself
return true;
}
}
@Override
public void onReceivedError (WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == ERROR_TIMEOUT) {
view.stopLoading(); // may not be needed
view.loadData(timeoutMessageHtml, "text/html", "utf-8");
}
}
});
答案 1 :(得分:10)
Marco W.是对的。
myWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
myWebView.loadUrl("file:///android_asset/custom_url_error.htm");
}
});
答案 2 :(得分:0)
我已尝试使用上述所有解决方案但其中没有一个正在运行。扭曲了我的代码并获得了解决方案:
package com.samnjor.tipsmaster;
import android.content.Context; import android.graphics.Bitmap;
import android.net.ConnectivityManager; import
android.net.NetworkInfo; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.webkit.WebSettings; import
android.webkit.WebView; import android.webkit.WebViewClient; import
android.widget.ProgressBar; import android.widget.TextView;
import com.google.android.gms.ads.AdListener; import
com.google.android.gms.ads.AdRequest; import
com.google.android.gms.ads.InterstitialAd;
public class TodayTips extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView webview ;
private ProgressBar spinner;
private String TAG = TodayTips.class.getSimpleName();
InterstitialAd mInterstitialAd;
final String noconnectionHtml = "Failed to connect ot the internet";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_today_tips);
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.adbig));
AdRequest adRequest = new AdRequest.Builder()
.build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
showInterstitial();
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
webview =(WebView)findViewById(R.id.webView);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
webview.setWebViewClient(new CustomWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
if(haveNetworkConnection()){
webview.loadUrl("http://you domain here");
} else {
webview.loadData(noconnectionHtml, "text/html", "utf-8"); //
}
}
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;
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(webview.INVISIBLE);
}
}
@Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(webview.VISIBLE);
super.onPageFinished(view, url);
}
}`` }