首先要在AndroidManifest.xml加上权限:
判断有无网络连接:
ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
//检查网络连接
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if(info ==null|| !mConnectivity.getBackgroundDataSetting()) {
returnfalse;
}
ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
//检查网络连接
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return false;
}
检查网络类型