Detect connectivity changes on Android 7.0 Nougat when app is in foreground

前端 未结 5 1450
我寻月下人不归
我寻月下人不归 2020-12-17 22:29

Nougat changed the way it handles CONNECTIVITY_CHANGED intents (basically ignoring it, forcing devs to use the job scheduler) so this leaves me wondering:

If I have

5条回答
  •  眼角桃花
    2020-12-17 23:16

    public class ConnectivityReceiver
    {
    
    public static boolean getWifiStatus(Context context)
    {
        // To get System Connectivity status
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    
    
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork)
        {
            // Check For Wifi Status
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return true;
            else
                return false;
        }
    
        return false;
    }
    
    public class NetworkMgr extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        ConnectivityReceiver cf = new ConnectivityReceiver();
        boolean status = cf.getWifiStatus(context);
    
        if(status)
        {
            Toast.makeText(context,"Wifi Connection is On.", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(context,"Wifi Connection is Off.", Toast.LENGTH_SHORT).show();
        }
      }
    }
    

提交回复
热议问题