Example usage for android.net ConnectivityManager TYPE_WIFI

List of usage examples for android.net ConnectivityManager TYPE_WIFI

Introduction

In this page you can find the example usage for android.net ConnectivityManager TYPE_WIFI.

Prototype

int TYPE_WIFI

To view the source code for android.net ConnectivityManager TYPE_WIFI.

Click Source Link

Document

A WIFI data connection.

Usage

From source file:com.lance.commu.fragment.BaseActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("base onCreate ");
    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    if (savedInstanceState == null) {
        FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
        mFrag = new MenuListFragment();
        t.replace(R.id.menu_frame, mFrag);
        t.commit();//from  ww  w  . j  a va  2 s .c o m
    } else {
        mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
    }
    // customize the SlidingMenu  
    SlidingMenu sm = getSlidingMenu();
    sm.setShadowWidthRes(R.dimen.shadow_width);
    sm.setShadowDrawable(R.drawable.shadow);
    sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    sm.setFadeDegree(0.35f);
    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    sm.setBehindOffset(400);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ////////////////////////////////////////
    connect = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); //  
    requestURL1 = "http://121.157.84.63:8080/lance/androidFriendList.jsp"; //    

    //       
    //  OracleDB    
    //   ListView   
    getContactData();

    //Sqlite DB  
    //Oracle DB   Sqlite  
    //     Oracle DB   
    //Sqlite     
    //      Oracle DB  Sqlite 
    try {
        db_Handler = DB_Handler.open(this);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("db open ");
    }

    //       
    //Oracle DB  
    if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
            || connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                    .getState() == NetworkInfo.State.CONNECTED) {
        if (flag == true) {
            new Networking1().execute();
            System.out.println(" ");
            flag = false; //          
                          // Sqlite ListView  
        }

    }

    else {
        Toast toast = Toast.makeText(BaseActivity.this, " ", Toast.LENGTH_SHORT);
        toast.show();
    }

    System.out.println("base onCreate ");
}

From source file:etsii_upm.obdii.EnviarService.java

private void gestionThread() {

    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
    if (mNetworkInfo == null) {
        // Pruebas :
        // consolaUpdate("iReceive: sin conexion a internet");

        if (rThread) {
            enviarThread.interrupt();/*from  ww w  . ja  v  a  2s.c o m*/
            rThread = false;
            consolaUpdate(this.getString(R.string.no_enviamos));
        }

        return;
    } else {

        if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            // Pruebas :
            // consolaUpdate("iReceive: conexion WIFI network");

            if (!rThread) {
                enviarThread = new EnviarThread(mHandler);
                enviarThread.start();
                rThread = true;
                consolaUpdate(this.getString(R.string.enviamos));
            }

        } else if (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            // Pruebas :
            // consolaUpdate("iReceive: conexion MOBILE");

            if (enviarCon3g) {
                if (!rThread) {
                    enviarThread = new EnviarThread(mHandler);
                    enviarThread.start();
                    rThread = true;
                    consolaUpdate(this.getString(R.string.enviamos));
                }
            } else {
                if (rThread) {
                    enviarThread.interrupt();
                    rThread = false;
                    consolaUpdate(this.getString(R.string.no_enviamos));
                }
            }

        }
    }

}

From source file:com.just.agentweb.AgentWebUtils.java

public static int checkNetworkType(Context context) {

    int netType = 0;
    //?/*from w  w w.  j a  va2  s . c  om*/
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //?NetworkInfo
    @SuppressLint("MissingPermission")
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return netType;
    }
    switch (networkInfo.getType()) {
    case ConnectivityManager.TYPE_WIFI:
    case ConnectivityManager.TYPE_WIMAX:
    case ConnectivityManager.TYPE_ETHERNET:
        return 1;

    case ConnectivityManager.TYPE_MOBILE:
        switch (networkInfo.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_LTE: // 4G
        case TelephonyManager.NETWORK_TYPE_HSPAP:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
            return 2;
        case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
            return 3;

        case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return 4;

        default:
            return netType;
        }

    default:

        return netType;
    }

}

From source file:org.openremote.android.console.net.ORNetworkCheck.java

/**
 * Detects the current WiFi status.//from  w  w w  . ja va2  s. c  o  m
 *
 * @param ctx     global Android application context
 *
 * @return true if WiFi is available, false otherwise
 */
private static boolean canReachWifiNetwork(Context ctx) {

    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (!wifiManager.isWifiEnabled() || wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
        Log.i(LOG_CATEGORY, "WiFi not enabled or WiFi network not detected.");

        return false;
    }

    NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (!wifiNetworkInfo.isAvailable()) {
        Log.i(LOG_CATEGORY, "Wifi network detected but wasn't available.");

        return false;
    }

    else {
        return true;
    }
}

From source file:net.mm2d.dmsexplorer.ServerListActivity.java

private Collection<NetworkInterface> getWifiInterface() {
    final NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected() || ni.getType() != ConnectivityManager.TYPE_WIFI) {
        return null;
    }/*from w  ww. j  a v a2s . co m*/
    final InetAddress address = getWifiInetAddress();
    if (address == null) {
        return null;
    }
    final Enumeration<NetworkInterface> nis;
    try {
        nis = NetworkInterface.getNetworkInterfaces();
    } catch (final SocketException e) {
        return null;
    }
    while (nis.hasMoreElements()) {
        final NetworkInterface nif = nis.nextElement();
        try {
            if (nif.isLoopback() || nif.isPointToPoint() || nif.isVirtual() || !nif.isUp()) {
                continue;
            }
            final List<InterfaceAddress> ifas = nif.getInterfaceAddresses();
            for (final InterfaceAddress a : ifas) {
                if (a.getAddress().equals(address)) {
                    final Collection<NetworkInterface> c = new ArrayList<>();
                    c.add(nif);
                    return c;
                }
            }
        } catch (final SocketException ignored) {
        }
    }
    return null;
}

From source file:io.lqd.sdk.model.LQDevice.java

private static String getInternetConnectivity(Context context) {
    if (LiquidTools.checkForPermission(permission.ACCESS_NETWORK_STATE, context)) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mNetwork = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mWifi != null && mWifi.isConnected()) {
            return "WiFi";
        } else if (mNetwork != null && mNetwork.isConnected()) {
            return "Cellular";
        } else {//from w w  w.  ja  v  a 2 s.  c  o  m
            return "No Connectivity";
        }
    } else {
        return "No ACCESS_NETWORK_STATE permission";
    }
}

From source file:com.popcorntime.apps.remote.utils.Utils.java

public static boolean isWIFIConnected(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (mWifi != null && mWifi.isConnected())
        return true;

    return false;
}

From source file:android.net.http.cts.ApacheHttpClientTest.java

private void disconnectWifiToConnectToMobile() throws InterruptedException {
    if (mHasWifi && mWifiManager.isWifiEnabled()) {
        ConnectivityActionReceiver connectMobileReceiver = new ConnectivityActionReceiver(
                ConnectivityManager.TYPE_MOBILE, State.CONNECTED);
        ConnectivityActionReceiver disconnectWifiReceiver = new ConnectivityActionReceiver(
                ConnectivityManager.TYPE_WIFI, State.DISCONNECTED);
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(connectMobileReceiver, filter);
        mContext.registerReceiver(disconnectWifiReceiver, filter);

        assertTrue(mWifiManager.setWifiEnabled(false));
        assertTrue(disconnectWifiReceiver.waitForStateChange());
        assertTrue(connectMobileReceiver.waitForStateChange());

        mContext.unregisterReceiver(connectMobileReceiver);
        mContext.unregisterReceiver(disconnectWifiReceiver);
    }//from  ww w . jav  a  2s . co  m
}

From source file:com.cyanogenmod.settings.device.LtoDownloadService.java

private boolean shouldDownload(boolean forceDownload) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    boolean hasConnection = false;

    if (info == null || !info.isConnected()) {
        if (ALOGV)
            Log.v(TAG, "No network connection is available for LTO download");
    } else if (forceDownload) {
        if (ALOGV)
            Log.v(TAG, "Download was forced, overriding network type check");
        hasConnection = true;/* w ww  . j a v a 2 s.c o  m*/
    } else {
        boolean wifiOnly = prefs.getBoolean(KEY_WIFI_ONLY, WIFI_ONLY_DEFAULT);
        if (wifiOnly && info.getType() != ConnectivityManager.TYPE_WIFI) {
            if (ALOGV) {
                Log.v(TAG, "Active network is of type " + info.getTypeName() + ", but Wifi only was selected");
            }
        } else {
            hasConnection = true;
        }
    }

    if (!hasConnection) {
        return false;
    }
    if (forceDownload) {
        return true;
    }
    if (!prefs.getBoolean(KEY_ENABLED, false)) {
        return false;
    }

    long now = System.currentTimeMillis();
    long lastDownload = LtoDownloadUtils.getLastDownload(this);
    long due = lastDownload + LtoDownloadUtils.getDownloadInterval(this);

    if (ALOGV) {
        Log.v(TAG, "Now " + now + " due " + due + "(" + new Date(due) + ")");
    }

    if (lastDownload != 0 && now < due) {
        if (ALOGV)
            Log.v(TAG, "LTO download is not due yet");
        return false;
    }

    return true;
}

From source file:org.wso2.iot.agent.services.NetworkInfoService.java

/**
 * Network data such as  connection type and signal details can be fetched with this method.
 *
 * @return String representing network details.
 * @throws AndroidAgentException//  w  ww  .ja  v  a  2s .c o  m
 */
public static String getNetworkStatus() throws AndroidAgentException {
    if (thisInstance == null) {
        Log.e(TAG, "Service not instantiated");
        throw new AndroidAgentException(TAG + " is not started.");
    }
    info = thisInstance.getNetworkInfo();
    String payload = null;
    if (info != null) {
        List<Device.Property> properties = new ArrayList<>();
        Device.Property property = new Device.Property();
        property.setName(Constants.Device.CONNECTION_TYPE);
        property.setValue(info.getTypeName());
        properties.add(property);

        if ((info.isConnected())) {
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                property = new Device.Property();
                property.setName(Constants.Device.MOBILE_CONNECTION_TYPE);
                property.setValue(info.getSubtypeName());
                properties.add(property);
            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                property = new Device.Property();
                property.setName(Constants.Device.WIFI_SSID);
                // NetworkInfo API of Android seem to add extra "" to SSID, therefore escaping it.
                property.setValue(String.valueOf(thisInstance.getWifiSSID()).replaceAll("\"", ""));
                properties.add(property);

                property = new Device.Property();
                property.setName(Constants.Device.WIFI_SIGNAL_STRENGTH);
                property.setValue(String.valueOf(thisInstance.getWifiSignalStrength()));
                properties.add(property);
            }
        }

        property = new Device.Property();
        property.setName(Constants.Device.MOBILE_SIGNAL_STRENGTH);
        property.setValue(String.valueOf(getCellSignalStrength()));
        properties.add(property);

        try {
            payload = mapper.writeValueAsString(properties);
        } catch (JsonProcessingException e) {
            String errorMsg = "Error occurred while parsing " + "network property property object to json.";
            Log.e(TAG, errorMsg, e);
            throw new AndroidAgentException(errorMsg, e);
        }
    }
    return payload;
}