代码摘要:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
ConnectivityManager cm;
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiConnected=cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true : false ;
boolean isGprsConnected=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ? true : false ;
需要添加权限:
<!-- 查询网络状态权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 修改手机连接网路状态权限 -->
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />