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:com.p2p.misc.DeviceUtility.java

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

    boolean isConnected = false;

    // wifi/*from w  w w  .jav  a2  s.  c o m*/
    State wifi = conMan.getNetworkInfo(1).getState();
    if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
        isConnected = true;
    }
    return isConnected;
}

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

public String availableConnectivity() {
    ConnectivityManager conMan = (ConnectivityManager) mactivity.getSystemService(Context.CONNECTIVITY_SERVICE);

    String nwrType = "";

    // wifi//from www  .j a v  a2s  .c  o  m
    State wifi = conMan.getNetworkInfo(1).getState();

    // mobile
    State mobile = null;
    try {
        mobile = conMan.getNetworkInfo(0).getState();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    if (mobile != null && mobile == State.CONNECTED || mobile == State.CONNECTING) {
        nwrType = "GSM";

    } else if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
        nwrType = "Wi-Fi";
    }

    return nwrType;
}

From source file:de.mangelow.throughput.NotificationService.java

private boolean getNetworkState(int type) {
    ConnectivityManager connect = null;
    connect = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connect != null) {
        NetworkInfo result = connect.getNetworkInfo(type);
        if (result != null && result.isConnected()) {
            return true;
        } else {/*from ww  w. j  a  v  a 2s. c  om*/
            return false;
        }
    } else
        return false;
}

From source file:com.rickendirk.rsgwijzigingen.ZoekService.java

@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean clusters_enabled = sp.getBoolean("pref_cluster_enabled", true);
    boolean alleenBijWifi = sp.getBoolean("pref_auto_zoek_wifi", false);
    boolean isAchtergrond = intent.getBooleanExtra("isAchtergrond", false);
    if (alleenBijWifi && isAchtergrond) {
        ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nwInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!nwInfo.isConnectedOrConnecting()) {
            //Later weer proberen: nu geen wifi
            setAlarmIn20Min();//from www. java2 s.  c o  m
            return;
        }
    }
    Wijzigingen wijzigingen = checkerNieuw(clusters_enabled);
    //Tracken dat er is gezocht
    OwnApplication application = (OwnApplication) getApplication();
    Tracker tracker = application.getDefaultTracker();

    if (isAchtergrond) {
        tracker.send(
                new HitBuilders.EventBuilder().setCategory("Acties").setAction("Zoeken_achtergrond").build());
        sendNotification(wijzigingen);
    } else {
        tracker.send(
                new HitBuilders.EventBuilder().setCategory("Acties").setAction("Zoeken_voorgrond").build());
        boolean isFoutMelding = wijzigingen.isFoutmelding();
        if (!isFoutMelding)
            wijzigingen.saveToSP(this);
        broadcastResult(wijzigingen, clusters_enabled);
    }
}

From source file:ch.bfh.instacircle.NetworkActiveActivity.java

/**
 * Checks whether the current network configuration is how it is supposed to
 * be/*  www  . ja v  a 2  s. c o  m*/
 */
public void checkNetworkState() {
    ConnectivityManager conn = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
    final String configuredSsid = preferences.getString("SSID", "N/A");
    final String password = preferences.getString("password", "N/A");

    NetworkInfo nInfo = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    String ssid = wifi.getConnectionInfo().getSSID();
    Log.d(TAG, "Currently active SSID: " + ssid);
    // Only check the state if this device is not an access point
    if (!wifiapmanager.isWifiAPEnabled(wifi)) {
        Log.d(TAG, "Configured SSID: " + configuredSsid);
        if (!(nInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED
                && nInfo.getState() == NetworkInfo.State.CONNECTED && ssid.equals(configuredSsid))) {
            if (repairInitiated == false && repairTime + 6000 < System.currentTimeMillis()) {
                repairInitiated = true;
                repairTime = System.currentTimeMillis();
                // create a dialog that ask whether you want to repair the
                // network connection
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle("InstaCircle - Network Connection Lost");
                alertDialog.setMessage(
                        "The connection to the network " + configuredSsid + " has been lost. Try to repair?");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Repair",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                adhoc.connectToNetwork(configuredSsid, password, NetworkActiveActivity.this,
                                        false);
                                repairInitiated = false;
                            }
                        });
                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Leave",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                WifiManager wifiman = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                new AdhocWifiManager(wifiman).restoreWifiConfiguration(getBaseContext());
                                new WifiAPManager().disableHotspot(wifiman, getBaseContext());
                                NotificationManager notificationManager = (NotificationManager) getSystemService(
                                        Context.NOTIFICATION_SERVICE);
                                notificationManager.cancelAll();
                                Intent stopIntent = new Intent(NetworkActiveActivity.this,
                                        NetworkService.class);
                                stopService(stopIntent);
                                Intent intent = new Intent(NetworkActiveActivity.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                        });
                alertDialog.show();
            }
        } else {
            repairInitiated = false;
        }
    }

}

From source file:com.wojtechnology.sunami.TheBrain.java

public boolean isSoundcloudEnabled() {
    if (!mMainPrefs.isSoundcloudEnabled)
        return false;
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Service.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo connection = connectivityManager.getActiveNetworkInfo();
    return wifi.isConnected() || (!mMainPrefs.forceOverWifi && connection != null && connection.isConnected());
}

From source file:org.smilec.smile.student.HttpMsgForStudent.java

private boolean isConnected() {
    boolean r = true;
    ConnectivityManager manager = (ConnectivityManager) main.getSystemService(Context.CONNECTIVITY_SERVICE);
    //boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    boolean isWifi = false;

    if (manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null)
        isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

    //Log.d(APP_TAG,"test if the wifi is connected wifi: " + isWifi + ", mobile: " + is3g);

    if (!isWifi) {
        r = false;//  w  w  w  . j  a  v a2  s.  c  o  m
    }

    return r;
}

From source file:jupiter.broadcasting.live.holo.JBPlayer.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Context ct = this;
    String ns = NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) getSystemService(ns);
    nReceiver = new BroadcastReceiver() {
        @Override//from  w w  w  .  jav a 2s  .co  m
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if (action.equalsIgnoreCase("PLAY_PAUSE")) {
                    if (mp.isPlaying()) {
                        mp.pause();
                        unregisterReceiver(nReceiver);
                        putNotificationUp(mp.isPlaying());
                    } else {
                        mp.start();
                        unregisterReceiver(nReceiver);
                        putNotificationUp(mp.isPlaying());
                    }
                } else if (action.equalsIgnoreCase("STOP")) {
                    unregisterReceiver(nReceiver);
                    mNotificationManager.cancel(NOTIFICATION_ID);
                    mp.stop();
                    start = false;
                }
            }
        }
    };

    //check if network notification is disabled in settings
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    notify = sharedPref.getBoolean("pref_sync_audio", false);

    mVideoCastManager = JBApplication.getVideoCastManager(this);
    mVideoCastManager.reconnectSessionIfPossible(this, true);

    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.mediaplayer);

    type = getIntent().getIntExtra("type", 3);
    title = getIntent().getStringExtra("title");
    pic = "http://jb4.cdn.scaleengine.net/wp-content/themes/jb2014/images/logo.png";
    hasit = HasIt(title, av);
    getSupportActionBar().setTitle(title);
    mDecorView = getWindow().getDecorView();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        mDecorView.setOnSystemUiVisibilityChangeListener(this);

    // going on different routes if coming from Catalogue/Home/EpisodeList
    offline = getIntent().getBooleanExtra("offline", false);
    String help = getIntent().getStringExtra("loc");
    live = ((help != null) && help.equalsIgnoreCase("-1"));
    if (!offline && !hasit) {
        pic = getIntent().getStringExtra("pic");
        aLink = getIntent().getStringExtra("aLink");
        vLink = getIntent().getStringExtra("vLink");
        theLink = aLink;
        GetSize newSize = new GetSize();
        newSize.execute();
    } else if (!live) {
        theLink = getIntent().getStringExtra("loc");
    } else {
        aLink = getIntent().getStringExtra("aLink");
        vLink = getIntent().getStringExtra("vLink");
        theLink = aLink;
    }

    iView = (FadeImageView) findViewById(R.id.thumb);
    RequestQueue mReqQue = Volley.newRequestQueue(getApplicationContext());
    ImageLoader mImageLoader = new ImageLoader(mReqQue, new BitmapLruCache());

    if (!offline) {
        iView.setImageUrl(pic, mImageLoader);
    }
    if (offline && !live) {
        switch (type) {
        case 0:
            SpinnerArray.add(getString(R.string.audio));
            break;
        case 1:
            SpinnerArray.add(getString(R.string.video));
            break;
        }
    } else {
        SpinnerArray.add(getString(R.string.audio));
        SpinnerArray.add(getString(R.string.video));
    }
    spinner = (Spinner) findViewById(R.id.AV);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            SpinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    tw = (TextView) findViewById(R.id.summary);
    tw.setMovementMethod(new ScrollingMovementMethod());
    tw.setText(getIntent().getStringExtra("sum"));

    play = (Button) findViewById(R.id.player);
    play.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            ConnectivityManager connectivity = (ConnectivityManager) getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            wifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if ((wifiInfo == null || wifiInfo.getState() != NetworkInfo.State.CONNECTED) && !hasit && notify) {
                AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(ct);
                myAlertDialog.setTitle(R.string.alert);
                myAlertDialog.setMessage(R.string.areyousure2);
                myAlertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        if (av == 1) //video
                            StartPlay(theLink);
                        else //audio
                            try {
                                StartPlayBackground(theLink);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                    }
                });
                myAlertDialog.setNegativeButton(getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                            }
                        });
                myAlertDialog.show();
            } else {
                if (av == 1) //video
                    StartPlay(theLink);
                else //audio
                    try {
                        StartPlayBackground(theLink);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

        }
    });

    down = (Button) findViewById(R.id.downer);
    if (offline || hasit) {
        down.setVisibility(View.INVISIBLE);
    }
    down.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final String url = theLink;
            //if wifi not connected, ask to make sure
            ConnectivityManager connectivity = (ConnectivityManager) getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            wifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if ((wifiInfo == null || wifiInfo.getState() != NetworkInfo.State.CONNECTED) && notify) {
                AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(ct);
                myAlertDialog.setTitle(R.string.alert);
                myAlertDialog.setMessage(R.string.areyousure2);
                myAlertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        DownLoad(url);
                    }
                });
                myAlertDialog.setNegativeButton(getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                            }
                        });
                myAlertDialog.show();
            } else {
                DownLoad(url);
            }

        }
    });
}

From source file:com.connectsdk.discovery.DiscoveryManager.java

/**
 * Start scanning for devices on the local network.
 *//*from w w  w  .  jav  a 2  s .  c o  m*/
public void start() {
    if (mSearching)
        return;

    if (discoveryProviders == null) {
        return;
    }

    mSearching = true;
    multicastLock.acquire();

    Util.runOnUI(new Runnable() {

        @Override
        public void run() {
            if (discoveryProviders.size() == 0) {
                registerDefaultDeviceTypes();
            }

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

            if (mWifi.isConnected()) {
                for (DiscoveryProvider provider : discoveryProviders) {
                    provider.start();
                }
            } else {
                Log.w("Connect SDK", "Wifi is not connected yet");

                Util.runOnUI(new Runnable() {

                    @Override
                    public void run() {
                        for (DiscoveryManagerListener listener : discoveryListeners)
                            listener.onDiscoveryFailed(DiscoveryManager.this,
                                    new ServiceCommandError(0, "No wifi connection", null));
                    }
                });
            }
        }
    });
}