Android用のWebアプリを作成したい。ボタンをクリックすると、アプリが Web ページを開きます。
インターネットに接続していれば、アプリは正常に動作しています。
インターネット接続を無効にすると、「Web ページが見つかりません」というメッセージが表示され、Web ページのリンクが表示されます。
私の質問は:
1 :ウェブページのリンクを保護する方法と
2 : Web Page Not Foundのエラーメッセージを無効にする方法
例が役立つと思います。
WebViewClient
エラーイベントを取得するために使用できます。load
、エラーHTML
ページコードサンプルは次のとおりです。
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestWebView extends Activity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_screen_image_layout);
webView = (WebView) findViewById(R.id.fullScreenImageWebView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.setWebViewClient(new WebViewClient() {
/*
* (non-Javadoc)
*
* @see
* android.webkit.WebViewClient#onReceivedError(android.webkit.WebView
* , int, java.lang.String, java.lang.String)
*/
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
loadError();
}
});
webView.loadUrl("http://www.google.com");
}
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);
webView.loadData(base64, "text/html; charset=utf-8", "base64");
System.out.println("loaded html");
}
}
まず、以下のコード スニペットを使用してインターネット接続を確認する必要があります
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
次に、ブール変数の値が true を返す場合は接続を確認し、false を返す場合はユーザーにエラー メッセージを表示します。
あなたが私の論理を理解してくれることを願っています
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
try {
webView.clearView();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/path_to_file");
super.onReceivedError(webView, errorCode, description, failingUrl);
}a
private WebViewClient client = new WebViewClient() {
// ... override whatever, including the onReceivedError method above
}
WebView webView = new WebView(context);
webView.setWebViewClient(client);
上記のコードで確認できます
webview でこれが許可されているかどうかはわかりませんが、まずインターネット接続を確認してください。接続が利用可能な場合は WebView を表示し、そうでない場合はエラー メッセージを表示します。