2014-10-08 33 views
0

我需要收集來自互聯網上的數據使用情況的信息,下面的代碼在C#我只使用返回0值。如何從互聯網收集數據使用信息到iPhone與Xamarin/monotouch

我正在使用正確的類嗎?

有一些我需要在Xamarin中添加的參考?

出現在單聲道3種類型的類,巧合的「MacOsIPv4InterfaceStatistics」接口始終返回固定值0。

https://github.com/mono/mono/blob/master/mcs/class/System/System.Net.NetworkInformation/IPv4InterfaceStatistics.cs

Win32IPv4InterfaceStatistics 
LinuxIPv4InterfaceStatistics 
MacOsIPv4InterfaceStatistics 


using System; 
using System.Net; 
using System.Net.NetworkInformation; 
using System.Runtime.InteropServices; 
using System.Threading; 
using MonoTouch.ObjCRuntime; 


namespace Taximetro { public delegate void Update(); 

public static class ControleUso 
{ 
    public static long controller; 

    public static long BytesReceivedWiFi; 
    public static long BytesSentWiFi; 

    public static long BytesReceived3G; 
    public static long BytesSent3G; 

    public static Update UpdateMethod; 

    public static void DoWork() 
    {  
     foreach (var netint in NetworkInterface.GetAllNetworkInterfaces()) { 
      var stats = netint.GetIPv4Statistics(); 

      //WiFi 
      //if (netint.Name.StartsWith ("en")) { 
       BytesReceivedWiFi += stats.BytesReceived; 
       BytesSentWiFi += stats.BytesSent; 
      //} 

      //3G 
      if (netint.Name.StartsWith ("pdp_ip")) { 
       BytesReceived3G += stats.BytesReceived; 
       BytesSent3G += stats.BytesSent; 
      } 
     } 
     controller++; 
     if (UpdateMethod != null) { 
      UpdateMethod(); 
     } 
     Thread.Sleep (1000); 
    } 
} 

} 
+0

AFAIK,iOS不公開任何公共API,允許您從設備收集這些數據。 – Jason 2014-10-08 12:15:47

+0

此鏈接顯示類似代碼寫在ObjC https://github.com/pkjmesra/DataUsage 但需要寫在我的項目在C# - Xamarin。 – rcaratchuk 2014-10-08 12:22:46

回答

0

注意,數據是UINT和不長,這意味着它將每4GB數據溢出。

編輯:經過一些谷歌搜索後,我注意到這個數據在iOS重啓時重置。只是有些人可能會使用它。

public enum InterfaceTypes 
{ 
    Wifi, 
    Cellular 
} 

public class DataUsage 
{ 
    [DllImport ("libc")] 
    static extern int getifaddrs (out IntPtr ifap); 

    [DllImport ("libc")] 
    static extern void freeifaddrs (IntPtr ifap); 

    const int AF_LINK = 18; 

    struct ifaddrs 
    { 
     public IntPtr ifa_next; 
     public string ifa_name; 
     public uint ifa_flags; 
     public IntPtr ifa_addr; 
     public IntPtr ifa_netmask; 
     public IntPtr ifa_dstaddr; 
     public IntPtr ifa_data; 
    } 

    public InterfaceTypes InterfaceType { 
     get; 
     private set; 
    } 

    public uint BytesSent { 
     get; 
     private set; 
    } 

    public uint BytesReceived { 
     get; 
     private set; 
    } 

    public static List<DataUsage> GetAllNetworkInterfacesUsage() 
    { 
     IntPtr ifap; 
     if (getifaddrs (out ifap) != 0) 
      throw new SystemException ("getifaddrs() failed"); 

     List<DataUsage> usages = new List<DataUsage>(); 
     try { 
      IntPtr next = ifap; 
      while (next != IntPtr.Zero) { 
       ifaddrs addr = (ifaddrs)Marshal.PtrToStructure (next, typeof(ifaddrs)); 
       next = addr.ifa_next; 
       string name = addr.ifa_name; 
       if (addr.ifa_addr != IntPtr.Zero) { 
        if (Marshal.ReadByte (addr.ifa_addr, 1) == AF_LINK) { 
         if (!name.StartsWith ("en") && !name.StartsWith ("pdp_ip")) 
          continue; 
         usages.Add (new DataUsage() { 
          InterfaceType = name.StartsWith ("en") ? InterfaceTypes.Wifi : InterfaceTypes.Cellular, 
          BytesReceived = (uint)Marshal.ReadInt32 (addr.ifa_data, 40), 
          BytesSent = (uint)Marshal.ReadInt32 (addr.ifa_data, 44) 
         }); 
        } 
       } 
      } 

     } finally { 
      freeifaddrs (ifap); 
     } 
     return usages; 
    } 
} 
+0

Quero agradecer pela resposta。 – rcaratchuk 2014-10-14 00:57:51

相關問題