2016-05-23 67 views
1

你好我使用的Android Studio和目前正在WebView中應用程序的網站沒有互聯網信息的Android的WebView當沒有互聯網顯示自定義的HTML頁面或

如何檢查網絡負載和而不是顯示默認的錯誤 - 網頁不可用,而不是我可以顯示一些圖形圖像?

我的代碼

Activity_fullscreen.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#0099cc" 
tools:context="com.croogster.android.FullscreenActivity"> 

<!-- The primary full-screen view. This can be replaced with whatever view 
    is needed to present your content, e.g. VideoView, SurfaceView, 
    TextureView, etc. --> 
<TextView 
    android:id="@+id/fullscreen_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:keepScreenOn="true" 
    android:text="@string/dummy_content" 
    android:textColor="#33b5e5" 
    android:textSize="50sp" 
    android:textStyle="bold" /> 

<!-- This FrameLayout insets its children based on system windows using 
    android:fitsSystemWindows. --> 
<LinearLayout 
    android:id="@+id/fullscreen_content_controls" 
    style="?metaButtonBarStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center_horizontal" 
    android:background="@color/black_overlay" 
    android:orientation="horizontal" 
    tools:ignore="UselessParent"> 

</LinearLayout> 

<WebView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/webView" 
    android:layout_gravity="left|bottom" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

</FrameLayout> 

FullscreenActivity.java

public class FullscreenActivity extends AppCompatActivity { 
    /** 
    * Whether or not the system UI should be auto-hidden after 
    * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. 
    */ 
    private static final boolean AUTO_HIDE = true; 

    /** 
    * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after 
    * user interaction before hiding the system UI. 
    */ 
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; 

    /** 
    * Some older devices needs a small delay between UI widget updates 
    * and a change of the status and navigation bar. 
    */ 
    private static final int UI_ANIMATION_DELAY = 300; 
    private final Handler mHideHandler = new Handler(); 
    private View mContentView; 
    private final Runnable mHidePart2Runnable = new Runnable() { 
     @SuppressLint("InlinedApi") 
     @Override 
     public void run() { 
      // Delayed removal of status and navigation bar 

      // Note that some of these constants are new as of API 16 (Jelly Bean) 
      // and API 19 (KitKat). It is safe to use them, as they are inlined 
      // at compile-time and do nothing on earlier devices. 
      mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE 
        | View.SYSTEM_UI_FLAG_FULLSCREEN 
        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 
     } 
    }; 
    private View mControlsView; 
    private final Runnable mShowPart2Runnable = new Runnable() { 
     @Override 
     public void run() { 
      // Delayed display of UI elements 
      ActionBar actionBar = getSupportActionBar(); 
      if (actionBar != null) { 
       actionBar.show(); 
      } 
      mControlsView.setVisibility(View.VISIBLE); 
     } 
    }; 
    private boolean mVisible; 
    private final Runnable mHideRunnable = new Runnable() { 
     @Override 
     public void run() { 
      hide(); 
     } 
    }; 
    /** 
    * Touch listener to use for in-layout UI controls to delay hiding the 
    * system UI. This is to prevent the jarring behavior of controls going away 
    * while interacting with activity UI. 
    */ 
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (AUTO_HIDE) { 
       delayedHide(AUTO_HIDE_DELAY_MILLIS); 
      } 
      return false; 
     } 
    }; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 



     setContentView(R.layout.activity_fullscreen); 
     WebView view = (WebView) this.findViewById(R.id.webView); 
     view.setWebViewClient(new WebViewClient()); 
     view.loadUrl("https://croogster.com"); 
     view.getSettings().setJavaScriptEnabled(true); 

     mVisible = true; 
     mControlsView = findViewById(R.id.fullscreen_content_controls); 
     mContentView = findViewById(R.id.fullscreen_content); 


     // Set up the user interaction to manually show or hide the system UI. 
     mContentView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       toggle(); 
      } 
     }); 

     // Upon interacting with UI controls, delay any scheduled hide() 
     // operations to prevent the jarring behavior of controls going away 
     // while interacting with the UI. 



    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 

     // Trigger the initial hide() shortly after the activity has been 
     // created, to briefly hint to the user that UI controls 
     // are available. 
     delayedHide(100); 
    } 

    private void toggle() { 
     if (mVisible) { 
      hide(); 
     } else { 
      show(); 
     } 
    } 

    private void hide() { 
     // Hide UI first 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.hide(); 
     } 
     mControlsView.setVisibility(View.GONE); 
     mVisible = false; 

     // Schedule a runnable to remove the status and navigation bar after a delay 
     mHideHandler.removeCallbacks(mShowPart2Runnable); 
     mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY); 
    } 

    @SuppressLint("InlinedApi") 
    private void show() { 
     // Show the system bar 
     mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); 
     mVisible = true; 

     // Schedule a runnable to display UI elements after a delay 
     mHideHandler.removeCallbacks(mHidePart2Runnable); 
     mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY); 
    } 

    /** 
    * Schedules a call to hide() in [delay] milliseconds, canceling any 
    * previously scheduled calls. 
    */ 
    private void delayedHide(int delayMillis) { 
     mHideHandler.removeCallbacks(mHideRunnable); 
     mHideHandler.postDelayed(mHideRunnable, delayMillis); 
    } 
} 

和我AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.croogster.android"> 

    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 


     <activity android:name=".Splashscreen" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


     <activity 
      android:name=".FullscreenActivity" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:label="@string/app_name" 
      android:theme="@style/FullscreenTheme"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

幫我

+1

可能的重複[防止WebView顯示「網頁不可用」](http://stackoverflow.com/questions/6552160/prevent-webview-from-displaying-web-page-not-available) –

+0

檢查Internet連接,如果它不可用,然後從資產中設置另一個HTML頁面,並簡單地顯示到您的webview。 – InsaneCat

回答

0

可以使用的WebView客戶端。

webView.setWebViewClient(new WebViewClient() { 

     @SuppressWarnings("deprecation") 
     @Override 
     public void onReceivedError(WebView view, int errorCode, 
            String description, String failingUrl) { 

      // TODO Auto-generated method stub 
      super.onReceivedError(view, errorCode, description, failingUrl); 

      webView 
        .loadData(
          "<div>Please check your internet connection.</div>", 
          "text/html", "UTF-8"); 

     } 

     @TargetApi(android.os.Build.VERSION_CODES.M) 
     @Override 
     public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) { 
      // Redirect to deprecated method, so you can use it in all SDK versions 
      onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString()); 

     } 

    }); 
+0

試過這個,但顯示錯誤,不能設置webview客戶端太 和覆蓋沒有onReceivedError語句在我的android工作室:( – Mahir

0

你應該設置你的WebView一個WebViewClient:

MyWebViewClient mWebViewClient; 
WebView mWebView; 
mWebView.setWebViewClient(mWebViewClient); 

這WebViewClient應該延伸WebViewClient:

class MyWebViewClient extends WebViewClient{} 

在這個類,你都能夠覆蓋onReceivedError,該報告錯誤的是發生(其中之一是當你沒有任何連接(我認爲這是errorCode -2):

@Override 
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
    super.onReceivedError(view, errorCode, description, failingUrl); 

    Log.d("error code", " " + String.valueOf(errorCode)); 

    if (errorCode == -2)                //errorCode 2 -> Server or proxy hostname lookup failed (no connection) 
    // show to the user whatever you want to show, for example, a local html page: 
    view.loadUrl("file:///android_asset/localhtmlpagenoconnection.html"); 
    } 

希望它可以幫助你。

+0

抱歉,不能理解它,請你解釋我在哪裏添加這些代碼或者你可以連接teamviewer到我的電腦,並告訴我它是如何完成的,真的很感激它 – Mahir

相關問題