Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

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

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:free.yhc.netmbuddy.utils.Utils.java

/**
 * Is any available active network at this device?
 * @return/*from  www. j av  a  2  s .  c  om*/
 */
public static boolean isNetworkAvailable() {

    ConnectivityManager cm = (ConnectivityManager) getAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni;

    if (isPrefUseWifiOnly())
        ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    else
        ni = cm.getActiveNetworkInfo();

    if (null != ni)
        return ni.isConnectedOrConnecting();
    else
        return false;
}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    if (context != null) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        try {//from   w  ww  . j a v a  2 s.  c om

            if (cm != null) {
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null) {

                    isNetworkAvailable = netInfo.isConnected();
                    //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                    //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
                }
            }
        } catch (Exception ex) {
            //Log.e("Network Avail Error", ex.getMessage());

        }
        //        check for wifi also
        if (!isNetworkAvailable) {

            WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            State wifi = cm.getNetworkInfo(1).getState();
            if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
                isNetworkAvailable = true;
                //Log.d("WIFI NETWORK<<","WIFI connected");
            } else {

                isNetworkAvailable = false;
                // Log.d("WIFI Network<<","WIFI not connected");
            }

        }
    }

    return isNetworkAvailable;

}

From source file:org.adblockplus.android.AdblockPlus.java

/**
 * Checks if device has a WiFi connection available.
 *///from w w w.  j a va  2  s  .co  m
public static boolean isWiFiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}

From source file:com.frostwire.android.gui.NetworkManager.java

private boolean isNetworkTypeUp(ConnectivityManager connectivityManager, final int networkType) {
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

From source file:free.yhc.feeder.model.Utils.java

/**
 * Is any available active network at this device?
 * @return/*  w  w  w  .ja v a  2  s  .com*/
 */
public static boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) Environ.getAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni;

    if (isPrefUseWifiOnly())
        ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    else
        ni = cm.getActiveNetworkInfo();

    if (null != ni)
        return ni.isConnectedOrConnecting();
    else
        return false;
}

From source file:com.tomeokin.archsample.data.remote.NetworkReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences preferences = ArchApplication.getApp()
            .getSharedPreferences(CollectManager.PREF_COLLECTION, Context.MODE_PRIVATE);
    final boolean haveCollection = preferences.getBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false);
    final int count = preferences.getInt(CollectManager.NEW_COLLECTION_COUNT, 0);
    if (!haveCollection || count <= 0) {
        return;//from w w w.  j  a v  a2  s  .  c o  m
    }

    Logger.t("NetworkReceiver").i("connection state change");
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi.isConnected()) {
            Logger.t("NetworkReceiver").i("wifi is connected");
            //List<Collection> collections = CollectManager.getInstance()
            //    .getCollectionsFromPref();

            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
                    0);
            Notification notification = new NotificationCompat.Builder(context)
                    .setTicker("have new collections").setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(count + " new collections")
                    //.setContentText("have new collections")
                    .setContentIntent(pi).setAutoCancel(true).build();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(ACTION_SHOW_NOTIFICATION_REQUEST_CODE, notification);
            preferences.edit().putBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false).apply();
        }
    }
}

From source file:com.example.qrpoll.Refresh.java

/**
 * sprawdza, czy w urzadzeniu wlaczona jest obsluga danych pakietowych
 * @return//w  w w.j a  va 2 s . c o m
 */
public boolean checkNetwork() {
    ConnectivityManager cm = (ConnectivityManager) activity.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return ni.isConnected();
}

From source file:at.metalab.donarsson.screeninvader.InvadeScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected()) {
        //TODO: Check if we know a ScreenInvader on this network
        Intent intent = getIntent();// ww w.  j  a  v  a2s .c om
        String type = intent.getType();
        if (type.startsWith("text/")) {
            String text = intent.getStringExtra(Intent.EXTRA_TEXT);
            Pattern pattern = Patterns.WEB_URL;
            Matcher matcher = pattern.matcher(text);
            while (matcher.find()) {
                String url = matcher.group();
                new PostUrlTask().execute(url);
            }
        } //TODO: Add support for other types (file upload)
    } else {
        //TODO: Display a prompt to connect to a WiFi
        Toast.makeText(getApplicationContext(), getString(R.string.no_wifi_toast), Toast.LENGTH_LONG).show();
    }
    finish();
}

From source file:com.johan.vertretungsplan.background.VertretungsplanService.java

@Override
protected void onHandleIntent(Intent intent) {
    context = this;
    settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    extras = intent.getExtras();/* w w  w. jav a  2 s.c  o  m*/
    Gson gson = new Gson();

    boolean autoSync;
    try {
        autoSync = extras.getBoolean("AutoSync");
    } catch (NullPointerException e) {
        autoSync = false;
    }

    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    //wifi
    State wifi = conMan.getNetworkInfo(1).getState();

    if (wifi == NetworkInfo.State.CONNECTED || autoSync == false
            || settings.getBoolean("syncWifi", false) == false) {

        Log.d("Vertretungsplan", "WiFi state: " + wifi);
        Log.d("Vertretungsplan", "autoSync: " + autoSync);
        Log.d("Vertretungsplan", "syncWifi: " + Boolean.valueOf(settings.getBoolean("syncWifi", false)));

        Log.d("Vertretungsplan", "Vertretungsplan wird abgerufen");

        try {
            BaseParser parser = ((VertretungsplanApplication) getApplication()).getParser();

            if (parser == null)
                return;

            Vertretungsplan v = parser.getVertretungsplan();
            settings.edit().putString("Vertretungsplan", gson.toJson(v)).commit();

            AppWidgetManager mgr = AppWidgetManager.getInstance(this);
            int[] ids = mgr.getAppWidgetIds(new ComponentName(this, VertretungsplanWidgetProvider.class));
            new VertretungsplanWidgetProvider().onUpdate(this, mgr, ids);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        } catch (VersionException e) {
        } catch (UnauthorizedException e) {
        }
    }
}

From source file:com.swisscom.safeconnect.fragment.InfoFragment.java

private String getConnectedSSID() {
    String ssid = null;//from   ww  w .j a v  a2  s.c om
    ConnectivityManager connManager = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
            ssid = connectionInfo.getSSID();
        }
    }
    return ssid;
}