2016-05-08 2 views
-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(); 
 
    } 
 
}

答えて

0

利用を進めるアンドロイドスタジオを使用しています。

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(); 
} 

} 
+0

申し訳ブラザー、私はこれを設定するかどうかは明確ではないです。読み込みコードを削除する必要がありますか?私は努力していません。このページの全部を追加してください、または私のコードを編集してください。あなたの返事をありがとう –

関連する問題