网页不可用nett :: err_internet_disconnected

时间:2016-10-22 18:56:10

标签: android

我的应用显示此页面

when no internet is there

instead of that I want set a page like this 并且当互联网回来时,它应该重定向用户离开的地方。

我的代码是 主要活动java

public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private LinearLayout layoutProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
    webView.setVisibility(View.GONE);
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setSupportZoom(true);
    settings.setDisplayZoomControls(false);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mms:") || url.startsWith("mmsto:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return false;
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            webView.setVisibility(View.VISIBLE);
            layoutProgress.setVisibility(View.GONE);
            progressBar.setIndeterminate(false);
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            layoutProgress.setVisibility(View.VISIBLE);
            progressBar.setIndeterminate(true);
            super.onPageStarted(view, url, favicon);
        }
    });
    if(isOnline()) {
        webView.loadUrl("http://www.mywebsite.com/");
    } else {
        String summary = "<html><body><b1><font color='red'><center>CHECK YOUR INTERNET CONNECTION\n</center><center><p> OR </p></center><center><p><a href=\"http://www.mywebsite.com\">TAP HERE TO REFRESH </a></p></center> </font></b1></centre></body></html>";
        webView.loadData(summary, "text/html", null);
        toast("No Internet Connection.");
    }
}
private void toast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView!=null && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
private boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}

}

我的活动主要

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
tools:context="com.my.website.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<LinearLayout
    android:id="@+id/layoutProgress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal|center_vertical" >
    <TextView
        android:id="@+id/textLoading"
        android:text="@string/label_loading"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_width="wrap_content"`enter code here`
        android:layout_height="wrap_content" />
    <View
        android:layout_width="fill_parent"
        android:layout_height="20dp" />
    <ProgressBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_gravity="center_vertical" />
</LinearLayout>

我的机器人清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.website" >
android:versionCode="33"
android:versionName="1.3" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/logo`enter code here`"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>


  </manifest>

请帮助我,我正在尝试过去2周,但我无法找到解决方案

0 个答案:

没有答案