简体   繁体   English

检查网络状态

[英]Checking network status

I would like to make a page that would automatically check the user's current network status when the application is started-up, and redirect the user to different page. 我想制作一个页面,该页面将在应用程序启动时自动检查用户的当前网络状态,并将用户重定向到其他页面。 When there is no network was connected, it will redirect the user to a specific page (eg. no_network_connected.xml). 当没有网络连接时,它将把用户重定向到特定页面(例如,no_network_connected.xml)。 Otherwise, it will bring the user to the (main.xml) page. 否则,它将把用户带到(main.xml)页面。

this method checks whether mobile is connected to internet and returns true if connected: 此方法检查移动设备是否已连接到互联网,如果已连接,则返回true:

private boolean isNetworkConnected() {
  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo ni = cm.getActiveNetworkInfo();
  if (ni == null) {
   // There are no active networks.
   return false;
  } else
   return true;
 }

in manifest, 明显地

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

from Android check internet connection Android检查互联网连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM