如果没有互联网连接,Android WebView离线消息

Android WebView offline message for if no internet connetion

本文关键字:WebView 离线 消息 Android 互联网 连接 如果没有      更新时间:2023-09-26

我正在使用WebView开发android应用程序。若设备并没有互联网连接,我想显示自定义消息。我是新来的。我做了这段代码,但在用HTML格式编写时没有显示自定义消息。请帮我一步一步走。我正在使用安卓工作室2.1感谢提前

package ideainnovative.bdsoeg;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class web extends AppCompatActivity {
    private WebView mywebview;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        mywebview = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebview.loadUrl("http://google.com/");
        mywebview.setWebViewClient(new WebViewClient());
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
    @Override
    public void onBackPressed() {
        if(mywebview.canGoBack()){
            mywebview.goBack();
        } else{
            super.onBackPressed();
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "web Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://google.com")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }
    private void loadError() {
        String html = "<html><body><table width='"100%'" height='"100%'" border='"0'" cellpadding='"0'" cellspacing='"0'">"
                + "<tr>"
                + "<td><div align='"center'"><font color='"red'" size='"20pt'">Your device don't have active internet connection</font></div></td>"
                + "</tr>" + "</table></html></body>";
        System.out.println("html " + html);
        String base64 = android.util.Base64.encodeToString(html.getBytes(),
                android.util.Base64.DEFAULT);
        mywebview.loadData(base64, "text/html; charset=utf-8", "base64");
        System.out.println("loaded html");
    }
    @Override
    public void onStop() {
        super.onStop();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "web Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://ideainnovative.bdsoeg/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

使用以下方法检查互联网连接:

public boolean isNetworkAvailable(final Context context) {
    final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
    return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}

在将url加载到网络视图之前,请检查互联网连接。

if (isNetworkAvailable(context)) {
    mywebview.loadUrl("http://google.com/");
} else {
    loadError()
}

现在你的活动:

public class web extends AppCompatActivity {
    private WebView mywebview;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        mywebview = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebview.setWebViewClient(new WebViewClient());
    if (isNetworkAvailable(context)) {
        mywebview.loadUrl("http://google.com/");
    } else {
        loadError()
    }
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
    @Override
    public void onBackPressed() {
        if(mywebview.canGoBack()){
            mywebview.goBack();
        } else{
            super.onBackPressed();
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "web Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://google.com")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }
    private void loadError() {
        String html = "<html><body><table width='"100%'" height='"100%'" border='"0'" cellpadding='"0'" cellspacing='"0'">"
                + "<tr>"
                + "<td><div align='"center'"><font color='"red'" size='"20pt'">Your device don't have active internet connection</font></div></td>"
                + "</tr>" + "</table></html></body>";
        System.out.println("html " + html);
        myWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
        System.out.println("loaded html");
    }
    @Override
    public void onStop() {
        super.onStop();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "web Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://ideainnovative.bdsoeg/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
public boolean isNetworkAvailable(final Context context) {
    final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
    return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}