-1
WebViewを使用してAndroidアプリを開発しています。デバイスにインターネット接続がない場合、カスタムメッセージを表示したい。私は新しいです。私はこのコードをしましたが、HTML形式で書くとカスタムメッセージを表示しません。ステップバイステップを手伝ってください。インターネット接続を確認しWebViewのためのURLをロードする前にインターネット接続がない場合のAndroid WebViewオフラインメッセージ
public boolean isNetworkAvailable(final Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
:私は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();
}
}
申し訳ブラザー、私はこれを設定するかどうかは明確ではないです。読み込みコードを削除する必要がありますか?私は努力していません。このページの全部を追加してください、または私のコードを編集してください。あなたの返事をありがとう –