android - How do I check if the user is connected to a wifi network or not? -
i know how check if wifi enabled or not.
code:
wifimanager wifi = (wifimanager)getsystemservice(context.wifi_service); if(wifi.iswifienabled()) { //code execution comes here }
but how find out if user connected nearby wifi network (or wifi network matter)?
edit: mean ask, if user has logged in wifi network after typing in password, able use wifi. there anyway check if has connected (logged in) wifi network?
you should able use connectivitymanager state of wifi adapter. can check if connected...
method check whether wifi conected or not :-
public static boolean checkwifi(activity activity) { log.d("checkwifi", "checkwifi"); connectivitymanager cm = (connectivitymanager) activity .getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); log.d("networkinfo", "networkinfo" + netinfo); if (netinfo != null && netinfo.isconnectedorconnecting()) { return true; } else if (netinfo != null && (netinfo.getstate() == networkinfo.state.disconnected || netinfo.getstate() == networkinfo.state.disconnecting || netinfo.getstate() == networkinfo.state.suspended || netinfo .getstate() == networkinfo.state.unknown)) { return false; } else { return false; } }
you need add permission in manifest
<uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.internet" />
good luck!!
Comments
Post a Comment