简体   繁体   中英

Detect when connected/disconntected to Wi-Fi

I want to know when phone lost connection with Wi-Fi. I know that I should use Broadcast Receiver. So I registered the Broadcast Receiver:

intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(wifiScanReceiver, intentFilter);

Then I created Broadcast Receiver:

public class WifiScanReceiver extends BroadcastReceiver {

    public WifiScanReceiver() {

    }

    public void onReceive(Context c, Intent intent) {
        Log.i("TAG", "trigged");
    }
}

Then I connect to Wi-Fi and when I turn on/off router - nothing happens. What am I doing wrong?

I have all permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Try with the ConnectivityManager class put this inside onReceive

ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = conMan.getActiveNetworkInfo();

    if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) 
        // Connection Successful 
    else
        // Connection  Failed 
  } 

As you have granted permissions in Manifest for Internet and Network access, that part is okay.

Further,

You may try ConnectivityManager Class for checking net /wifi connectivity to your device .

public boolean checkMobileInternetConn() {
        //Create object for ConnectivityManager class which returns network related info
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //If connectivity object is not null
        if (connectivity != null) {
            //Get network info - WIFI internet access
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (info != null) {
                //Look for whether device is currently connected to WIFI network
                if (info.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

(Refer this post for complete code.)

Try this n feel free to revert back :)

nothing happens. What am I doing wrong?

As far as I can see you have not declared the BroadcastReceiver , adding permission alone would not trigger your broadcast receiver.

My solution is based on declaring it via manifest, but that said your problem is I can not see you have declared broadcaster class in your code, so you can chose declare it in manifest or in your code.

So try to declare it and it should works. I have tested your code with both declaration and it works:

<receiver android:name=".WifiScanReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE"/>
    </intent-filter>
</receiver>

Or declare it programmatically new WifiScanReceiver() in onCreate method:

final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(new WifiScanReceiver(), filters);

You will need this also ( reference ) in your WifiScanReceiver:

public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

        if (isWiFi) {
            Log.i("TAG", "trigged");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM