監聽網路狀態

2022-01-20 07:49:20 字數 2642 閱讀 5355

using system;

using system.threading;

using system.runtime.interopservices;

namespace network

static networkhelper()

public static networkhelper getnetworkhelperinstance()

/// /// 檢查網路是否連通,有延遲

///

///

///

[dllimport("wininet.dll")]

private extern static bool internetgetconnectedstate(out int connectiondescription, int reservedvalue);

/// ///

///

/// 連線餓位址

/// flag_icc_force_connection

/// 0

[dllimport("wininet.dll")]

private extern static bool internetcheckconnection(string lpszurl, int dwflags, int dwreserved);

/// /// 檢查網路是否連通,需要啟動服務

///

///

[dllimport("sensapi.dll")]

private extern static bool isnetworkalive(out int connectiondescription);

/// /// 檢查是否能建立internet連線,vista不可用

///

///

/// 0

[dllimport("sensapi.dll")]

private extern static bool isdestinationreachable(string dest, intptr ptr);

/// /// 網際網路是否可用

///

///

public bool isinternetalive()

else

return false;

//}//else

// return false;

}return false;

}/// ///

///

///

public int checkintertnet()

else

return 0;

//}//else

// return false;

}return 0;

}/// /// 為networkstatuschanged事件處理程式提供資料

///

public class networkchangedeventargs : eventargs

public networkchangedeventargs(int status)

public bool isnetworkalive

private set

}public int isnetworkalive_int32

private set }}

/// /// 表示networkstatuschanged事件的方法

///

public delegate void networkchangedeventhandler(object sender, networkchangedeventargs e);

/// /// 網路狀態變更時觸發的事件

///

public event networkchangedeventhandler networkstatuschanged;

/// /// 網路狀態變更時觸發的事件

///

private void onnetworkstatuschanged(networkchangedeventargs e)

/// /// 監聽網路狀態

///

public void listennetworkstatus(synchronizationcontext context)

, null);

//onnetworkstatuschanged(new networkchangedeventargs(currentstatus));}}

, autoevent

, 0, listen_time_span);

false);

}/// /// 監聽網路狀態

///

public void listennetworkstatus(synchronizationcontext context, bool? b)

, null);}}

, autoevent

, 0, listen_time_span);

autoevent.waitone(5000, false);

}/// /// 停止監聽網路狀態

///

public void closelistennetworkstatus()

}#endregion

}

Android 監聽網路狀態

原文 網路狀態發生變化的時候,系統會發出 android net.conn.connectivity change 廣播 手機解鎖動作的時候,系統會發出android.intent.action.user present 廣播,我這裡為了在解鎖手機時獲取到,所以新增上手機解鎖事件廣播。註冊recei...

android監聽網路狀態

public class wificonnlisterer extends broadcastreceiver else if wifistate wifimanager.wifi state enabled 這個監聽wifi的連線狀態即是否連上了乙個有效無線路由,當上邊廣播的狀態是wifimana...

Android的網路狀態監聽

1 獲取當前網路資訊 想要獲取當前的網路資訊,就得獲取connectivitymanager,呼叫connectivitymanager的getactivenetworkinfo 方法,返回乙個networkinfo物件。這個networkinfo物件中的gettypename 方法,會返回當前的網...