Files
xxcpzs/XXCpzs.Android/Services/NetworkConnection.cs
2020-04-03 11:55:02 +08:00

171 lines
7.1 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Android.Content.PM;
using Android.OS;
using System;
using Xamarin.Forms;
using XXCpzs.Droid.Services;
using XXCpzs.Services;
using XXCpzs.Droid;
using Android.Net;
using Android.Locations;
using Android.Content;
using Android.Telephony;
using Xamarin.Essentials;
[assembly: Dependency(typeof(NetworkConnection))] //的作用是将该类依赖于程序集,以便在可移植平台底下实例化接口。
namespace XXCpzs.Droid
{
public class NetworkConnection : INetworkConnection
{
        //没有网络
        public const int NETWORKTYPE_INVALID = 0;
        //wap网络
        public const int NETWORKTYPE_WAP = 1;
        //2G网络
        public const int NETWORKTYPE_2G = 2;
        //3G和3G以上网络或统称为快速网络
        public const int NETWORKTYPE_3G = 3;
        //wifi网络
        public const int NETWORKTYPE_WIFI = 4;
public bool IsConnected { get; set; }
        //判断是否有网络连接
        public void CheckNetworkConnection()
{
var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
{
IsConnected = true;
}
else
{
IsConnected = false;
}
}
        //检查启用了网络位置提供商报告
        public bool isNetLocEnabled()
{
LocationManager locMgr = MainActivity.mContext.GetSystemService(Context.LocationService) as LocationManager;
return locMgr.IsProviderEnabled(LocationManager.NetworkProvider);
}
        //判断是否有WIFI连接
        public bool isWifiConnected(Context context)
{
if (context != null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
                //NetworkInfo mWiFiNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (mWiFiNetworkInfo != null)
{
return mWiFiNetworkInfo.IsAvailable;
}
}
return false;
}
        //判断Mobile网络是否可用
        public bool isMobileConnected(Context context)
{
if (context != null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
                //NetworkInfo mMobileNetworkInfo = mConnectivityManager.GetNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (mMobileNetworkInfo != null)
{
return mMobileNetworkInfo.IsAvailable;
}
}
return false;
}
        //检查飞行模式 - 我们要关闭飞机模式
        //public bool confirmAirplaneModeOff(Context context)
//{
//int airplaneSetting = Settings.System.GetInt(context.ContentResolver, Settings.System.AirplaneModeOn, 0);
            //int airplaneSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
            //return airplaneSetting == 0;
//}
        //确定网络位置提供程序是否真的可用
      //  public bool isNetlocUsable(Context context)
// {
// return isNetLocEnabled(context) && confirmAirplaneModeOff(context) && isWifiConnected(context) && isMobileConnected(context);
// }
        //判断是否是FastMobileNetWork将3G或者3G以上的网络称为快速网络
        private static bool isFastMobileNetwork(Context context)
{
TelephonyManager telephonyManager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);
switch (telephonyManager.NetworkType)
{
case NetworkType.OneXrtt:
return false; // ~ 50-100 kbps  
                case NetworkType.Cdma:
return false; // ~ 14-64 kbps  
                case NetworkType.Edge:
return false; // ~ 50-100 kbps  
                case NetworkType.Evdo0:
return true; // ~ 400-1000 kbps  
                case NetworkType.EvdoA:
return true; // ~ 600-1400 kbps  
                case NetworkType.Gprs:
return false; // ~ 100 kbps  
                case NetworkType.Hsdpa:
return true; // ~ 2-14 Mbps  
                case NetworkType.Hspa:
return true; // ~ 700-1700 kbps  
                case NetworkType.Hsupa:
return true; // ~ 1-23 Mbps  
                case NetworkType.Umts:
return true; // ~ 400-7000 kbps  
                case NetworkType.Ehrpd:
return true; // ~ 1-2 Mbps  
                case NetworkType.EvdoB:
return true; // ~ 5 Mbps  
                case NetworkType.Hspap:
return true; // ~ 10-20 Mbps  
                case NetworkType.Iden:
return false; // ~25 kbps  
                case NetworkType.Lte:
return true; // ~ 10+ Mbps  
                case NetworkType.Unknown:
return false;
default:
return false;
}
}
        //获取网络状态wifi,wap,2g,3g.
        /// <summary>
        /// context 上下文 
        /// return int 网络状态 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}
        /// {@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}* <p>{@link #NETWORKTYPE_WIFI} 
        /// </summary>
        public static int getNetWorkType(Context context)
{
int mNetWorkType = 0;
ConnectivityManager manager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
NetworkInfo networkInfo = manager.ActiveNetworkInfo;
if (networkInfo != null && networkInfo.IsConnected)
{
String type = networkInfo.TypeName;
if (type.Equals("WIFI".ToUpper()))
{
mNetWorkType = NETWORKTYPE_WIFI;
}
else if (type.Equals("MOBILE".ToUpper()))
{
String proxyHost = Proxy.DefaultHost;
                    //String proxyHost = android.net.Proxy.getDefaultHost();
                    //mNetWorkType = TextUtils.IsEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP;
}
}
else
{
mNetWorkType = NETWORKTYPE_INVALID;
}
return mNetWorkType;
}
}
}