wifi turned on, get connected to the network connection on the IP considered, there are ways to determine when the activity started it?
In addition, there is no way to monitor network connections and disconnect the wifi connection on the wifi network connection?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Determine whether WIFI connection:
ConnectivityManager manager = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
State wifi = manager.getNetworkInfo (ConnectivityManager.TYPE_WIFI) getState ();.
if (wifi == State.CONNECTED) {
/ / WIFI connected
}
Listen WifiManager.NETWORK_STATE_CHANGED_ACTION can get broadcast
WIFI network connection status changesListeners can get WIFI status change WifiManager.WIFI_STATE_CHANGED_ACTION broadcast (open, close, is opening, closing, unknown).
@ Override
public void onReceive (Context context, Intent intent) {
/ / TODO Auto-generated method stub
System.out.println (intent.getAction ());
if (intent.getAction (). equals (WifiManager.RSSI_CHANGED_ACTION))
{
/ / Signal intensity change
int strength = getStrength (context);
System.out.println ("current signal" + strength);
} Else if (intent.getAction (). Equals (WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
System.out.println ("network state change");
NetworkInfo info = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
if (info.getState (). equals (NetworkInfo.State.DISCONNECTED))
{
/ / WIFI network disconnect
} Else if (...) {
/ / Other states
}
} Else if (intent.getAction (). Equals (WifiManager.WIFI_STATE_CHANGED_ACTION))
{
/ / WIFI switch
int wifistate = intent.getIntExtra (WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
if (wifistate == WifiManager.WIFI_STATE_DISABLED)
{
/ / WIFI close
} Else if (...) {
/ / Other states
}
}
}
Need permission:
Reply:
@ Tantahe, thank you very much.
First code to determine whether WIFI connection, I tested the IP wifi connection, that is, that appeared in the notification bar wifi signal strength icon, only wifi == State.CONNECTED, if only wifi open, and there are a lot of nearby hot, but not one word on login, wifi state is not State.CONNECTED.
Second code, wifi turned on, and connected to a wifi hotspot, the state can be detected. But this NETWORK_STATE_CHANGED_ACTION, not just wifi login or exit trigger, 3G network login or exit may also trigger a few days I apply this test to see China Unicom 3G.
Reply:
Today, a 10 yuan flow customized package test it, Unicom 3G turned on and off, does not trigger NETWORK_STATE_CHANGED_ACTION, might move will not trigger. Thanks to @ tantahe again, I wish a happy holiday!
Reply:
WifiManager.NETWORK_STATE_CHANGED_ACTION WIFI network connection is only used to determine the state of the mobile network connection status change will not send this broadcast.
No comments:
Post a Comment