2017-12-18 10 views
0

私はアンドロイドプログラミングで初心者です。私のウェブサイト用のWebviewアプリを作成しました。インターネット接続が失われるまで問題はありませんでした。私はWebviewから "webpage not available"の表示なしのリンクをクリックしますか?インターネット接続が失われたときにWebviewでURLを開くのを防ぐ

私の期待される結果がありません:

を表示するトースト「いいえ、インターネット接続」まだ同じページに私はWebview

内のリンクをクリックしたときに、私はonReceivedErrorに使用しよう:

if (wv.canGoBack()) {wv.goBack();} 

でも、「ウェブページが利用できません」と表示され、前のページに戻る

PS:このコードショートースト "いいえ、インターネット接続"、それでも "ウェブページは利用できません" と表示

MyCode

MainActivity.java

import android.content.Context; 
import android.content.DialogInterface; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    WebView wv; 
    String URL; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(!isNetworkAvailable()){ 
      Toast.makeText(getApplicationContext(),"No Internet Connection",Toast.LENGTH_SHORT).show(); 
     }else{ 
      URL = "Https://resiongkir.dzakiyyah.com"; 
      wv = (WebView)findViewById(R.id.web); 
      wv.setWebViewClient(new WebViewClient(){ 
       @Override 
       public void onPageFinished(WebView view, String url){ 
        findViewById(R.id.imageView1).setVisibility(View.GONE); 
        findViewById(R.id.web).setVisibility(View.VISIBLE); 
       } 

       @Override 
       public void onReceivedError(WebView view, int errorCode,String description, final String failingUrl) { 
        Toast.makeText(getApplicationContext(), "No Internet Connection Or " + description , Toast.LENGTH_LONG).show(); 
        super.onReceivedError(view, errorCode, description, failingUrl); 
       } 
      }); 
      wv.getSettings().setJavaScriptEnabled(true); 
      wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
      wv.getSettings().setBuiltInZoomControls(true); 
      wv.loadUrl(URL); 
     } 
    } 

    private boolean isNetworkAvailable(){ 
     ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting(); 
    } 

    @Override 
    public void onBackPressed(){ 
     if(wv.canGoBack()){ 
      wv.goBack(); 
     }else{ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Anda yakin akan menutup ResiOngkir?") 
        .setCancelable(false) 
        .setPositiveButton("Ya", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          MainActivity.this.finish(); 
         } 
        }) 
        .setNegativeButton("Tidak", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.dzakiyyah.abu.resiongkir.MainActivity"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="228dp" 
     android:layout_height="216dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     android:contentDescription="@string/app_logo" 
     android:visibility="visible" 
     app:layout_constraintBottom_toTopOf="@+id/textView" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.975" 
     app:srcCompat="@drawable/muava" /> 


    <WebView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/web" 
     android:visibility="gone"/> 

</android.support.constraint.ConstraintLayout> 
+0

あなたが代わりに表示したいですか?乾杯は消えるが、何かが見せなければならないので? – Xenolion

+0

リンクをクリックした場合、ショーウェブページなしのトーストのみが表示されます –

+1

そのAPIは、ページロードを傍受する方法の1つを定義しています - webClientに 'shouldInterceptRequest'で独自の応答を代入させることによって。私はこれが本当にあなたのために働くだろうと思う。 –

答えて

3

ロードするには別のhtmlを指定する必要があります。それが簡単な場合はStringが必要です!このように:

@Override 
public void onReceivedError(WebView view, int errorCode,String description, final String failingUrl) { 
     Toast.makeText(getApplicationContext(), "No Internet Connection Or " + description , Toast.LENGTH_LONG).show(); 

     String error_summary = "<html><body> Dear User, An error happened <b>No internet</b> connection!.</body></html>"; 
     view.loadData(error_summary, "text/html", null); 
     super.onReceivedError(view, errorCode, description, failingUrl); 
} 

しかし、あなたは、Webページが同じURL(新しいものをロードしていない)を持つようにしたい場合は、最高のは、それINVISIBLEすることですこれはあなただけで作るあなたのコメントに応じて代わりのものをロードしたいものです空白ページにつながる目に見えないWebビュー!代わりの

onReceivedErrorで何かがこれを行うロード:

view.setVisibility(View.INVISIBLE); 

インターネット接続は、それが再びVISIBLE作ることを忘れないでください:

wv.setVisibility(View.VISIBLE); 
関連する問題