2014-09-20 52 views
1

我需要的是當用戶打開我的應用程序並且他或她沒有任何互聯網連接時,webview會顯示我在Photoshop中設計的屏幕。所以基本上不是標準的沒有網絡錯誤當應用程序無法連接時顯示「沒有互聯網連接」屏幕

這裏是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:clickable="false" 
    tools:context=".MyActivity"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:background="#ffffff" 
     android:gravity="center" 
     android:text="@string/vu" 
     android:textColor="#000000" 
     android:textSize="60sp" /> 

    <WebView 
     android:id="@+id/webView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/textView" /> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="ca-app-pub-3486775169258180/1572732950" /> 

</RelativeLayout> 

,這裏是java類 -

public class MyActivity extends Activity { 

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

     TextView tv = (TextView) findViewById(R.id.textView); 
     Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Light.ttf"); 
     tv.setTypeface(typeface); 
     WebView webView = (WebView) findViewById(R.id.webView); 
     webView.loadUrl(""); 
     webView.setWebViewClient(new WebViewClient()); 
     webView.setVerticalScrollBarEnabled(false); 
     webView.setOverScrollMode(View.OVER_SCROLL_NEVER); 
     // Look up the AdView as a resource and load a request. 
     AdView adView = (AdView) this.findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 
    } 
} 
+0

在哪裏是你的代碼嗎?你可以請出示嗎? – joao2fast4u 2014-09-20 18:10:28

+0

編輯它,看看 – CristianoWilson 2014-09-20 18:19:15

+0

你在哪裏檢查互聯網連接? – joao2fast4u 2014-09-20 18:21:36

回答

0

你可以嘗試這樣的:

private boolean haveNetworkConnection() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
} 
相關問題