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:piuk.blockchain.android.ui.AbstractWalletActivity.java

public final boolean isWifiEnabled() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (info.isConnected()) {
        return true;
    } else {//from   w ww. j ava2s .  c  om
        return false;
    }
}

From source file:eg.edu.alexu.alexandriauniversity.activity.ChannelsActivity.java

private void doFirstTime() {
    //add channels first run only
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
    if (!previouslyStarted) {

        //Check internet connection for now all times but next step will be first time only
        final ConnectivityManager connMgr = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi.isAvailable() && wifi.isConnected()) {
            Toast.makeText(this, "Adding Feeds over WIFI", Toast.LENGTH_LONG).show();
            addChannelFirstTime();//from   w  w  w .  j  a v a2 s  .  c  om
            SharedPreferences.Editor edit = prefs.edit();
            edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
            edit.apply();
        } else if (mobile.isAvailable() && mobile.isConnected()) {
            Toast.makeText(this, "Adding Feeds Over Mobile 3G ", Toast.LENGTH_LONG).show();
            addChannelFirstTime();
            SharedPreferences.Editor edit = prefs.edit();
            edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
            edit.apply();
        } else {
            Toast.makeText(this, "No Network WIFI/3G, Can't Add Feeds!", Toast.LENGTH_LONG).show();
            SharedPreferences.Editor edit = prefs.edit();
            edit.putBoolean(getString(R.string.pref_previously_started), Boolean.FALSE);
            edit.apply();
        }
    }
}

From source file:de.jeanpierrehotz.messagingapptest.MainActivity.java

/**
 * Diese Methode ermittelt, ob derzeit Internet verfgbar ist.
 *
 * @return ob Internet verfgbar ist/*from  www. j ava 2  s .c  o m*/
 */
private boolean isConnectionAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    Network[] networks = manager.getAllNetworks();
    for (Network net : networks) {
        if (manager.getNetworkInfo(net).isAvailable()) {
            return true;
        }
    }
    return false;
}

From source file:id.ridon.keude.UpdateService.java

/**
 * Check whether it is time to run the scheduled update.
 * We don't want to run if:/*from ww  w.  j a va 2 s . c  o  m*/
 *  - The time between scheduled runs is set to zero (though don't know
 *    when that would occur)
 *  - Last update was too recent
 *  - Not on wifi, but the property for "Only auto update on wifi" is set.
 * @return True if we are due for a scheduled update.
 */
private boolean verifyIsTimeForScheduledRun() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    long lastUpdate = prefs.getLong(Preferences.PREF_UPD_LAST, 0);
    String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
    int interval = Integer.parseInt(sint);
    if (interval == 0) {
        Log.d("Keude", "Skipping update - disabled");
        return false;
    }
    long elapsed = System.currentTimeMillis() - lastUpdate;
    if (elapsed < interval * 60 * 60 * 1000) {
        Log.d("Keude", "Skipping update - done " + elapsed + "ms ago, interval is " + interval + " hours");
        return false;
    }

    // If we are to update the repos only on wifi, make sure that
    // connection is active
    if (prefs.getBoolean(Preferences.PREF_UPD_WIFI_ONLY, false)) {
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState();
        if (wifi != NetworkInfo.State.CONNECTED && wifi != NetworkInfo.State.CONNECTING) {
            Log.d("Keude", "Skipping update - wifi not available");
            return false;
        }
    }
    return true;
}

From source file:org.opencv.samples.facedetect.PlayerViewDemoActivity.java

public void setIdArray() {
    // set the button unclickable while the playlist is downloaded
    playListButton.setEnabled(false);//from  w w  w. java 2s. c  o  m

    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (mWifi.isConnected()) {
        final String tag = getTag();
        String URL = "http://www.last.fm/tag/";
        URL = URL + tag + "/videos";

        ServerFetchAsyncTask down1 = new ServerFetchAsyncTask(URL, PlayerViewDemoActivity.this,
                new ServerFetchAsyncTask.MyCallBack() {
                    public void run(String[] sv) {
                        htmlSource1 = sv[0];
                        htmlSource2 = sv[1];

                        String separator = new String("<img src=");

                        // extract the first id array
                        String parts1[] = htmlSource1.split(separator);
                        // first and last 3 lines are not interesting
                        for (int i = 1; i < parts1.length - 3; i++) {
                            parts1[i] = parts1[i].substring(parts1[i].indexOf("vi/"),
                                    parts1[i].indexOf(".jpg"));
                            parts1[i] = parts1[i].substring(3, parts1[i].length() - 2);
                        }

                        String parts2[] = htmlSource2.split(separator);
                        for (int i = 1; i < parts2.length - 3; i++) {
                            parts2[i] = parts2[i].substring(parts2[i].indexOf("vi/"),
                                    parts2[i].indexOf(".jpg"));
                            parts2[i] = parts2[i].substring(3, parts2[i].length() - 2);
                        }

                        // final id array
                        songs = new ArrayList<String>();

                        InputStream inputStream = getResources().openRawResource(getRawId(tag));
                        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                        String s;
                        try {
                            while ((s = br.readLine()) != null) {
                                songs.add(s);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        for (int i = 1; i < parts1.length - 3; i++)
                            songs.add(parts1[i]);
                        for (int i = 1; i < parts2.length - 3; i++)
                            songs.add(parts2[i]);
                        // we use it in playvideoatselection() to get random
                        // id
                        playListButton.setEnabled(true);
                    }
                });
        Toast.makeText(PlayerViewDemoActivity.this, "Downloading id list...", Toast.LENGTH_SHORT).show();
        down1.execute();
    } else {
        Toast.makeText(PlayerViewDemoActivity.this, "Please turn wi-fi on", Toast.LENGTH_SHORT).show();
    }
}

From source file:mobile.tiis.appv2.LoginActivity.java

/**
 * function to check if there is an internet connectivity
 *///from  w ww  . j  a  va 2  s. com
public boolean isInternetAvailable() {
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }
    return false;
}

From source file:com.cybrosys.currency.CurrencyMain.java

public final boolean isConnection() {
    @SuppressWarnings("static-access")
    ConnectivityManager cm = (ConnectivityManager) getActivity()
            .getSystemService(getActivity().CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }//  www. j  ava 2s  . c  o  m
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }
    return false;
}

From source file:com.p2p.misc.DeviceUtility.java

public boolean isWifiOn() {
    ConnectivityManager conMan = (ConnectivityManager) mactivity.getSystemService(Context.CONNECTIVITY_SERVICE);

    // wifi/*from   ww  w  .j a v  a 2  s  . com*/
    State wifi = conMan.getNetworkInfo(1).getState();
    if (wifi == State.CONNECTED || wifi == State.CONNECTING || wifi == State.DISCONNECTING
            || wifi == State.DISCONNECTED) {

        return true;
    }
    return false;
}

From source file:org.dvbviewer.controller.ui.fragments.ChannelPager.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);//from  w  w  w.  j a v  a2 s  .  co  m
    // setRetainInstance(true);
    mAdapter = new PagerAdapter(getChildFragmentManager(), mGroupCursor);
    ConnectivityManager connManager = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    mNetworkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    prefs = new DVBViewerPreferences(getActivity());
    showGroups = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_GROUPS, false);
    showExtraGroup = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_ALL_AS_GROUP, false);
    showFavs = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_USE_FAVS, false);
    showNowPlaying = prefs.getPrefs().getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_NOW_PLAYING, true);
    showNowPlayingWifi = prefs.getPrefs()
            .getBoolean(DVBViewerPreferences.KEY_CHANNELS_SHOW_NOW_PLAYING_WIFI_ONLY, true);
}

From source file:com.hijacker.MainActivity.java

static boolean internetAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    return connectivityManager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED
            || connectivityManager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED;
}