当应用程序无法连接时显示“没有互联网连接”屏幕
问题描述:
我需要的是当用户打开我的应用程序并且他或她没有任何互联网连接时,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);
}
}
答
你可以尝试这样的:
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;
}
在哪里是你的代码吗?你可以请出示吗? – joao2fast4u 2014-09-20 18:10:28
编辑它,看看 – CristianoWilson 2014-09-20 18:19:15
你在哪里检查互联网连接? – joao2fast4u 2014-09-20 18:21:36