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:android.net.http.cts.ApacheHttpClientTest.java

private void connectToWifi() throws InterruptedException {
    if (!mWifiManager.isWifiEnabled()) {
        ConnectivityActionReceiver receiver = new ConnectivityActionReceiver(ConnectivityManager.TYPE_WIFI,
                State.CONNECTED);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(receiver, filter);

        assertTrue(mWifiManager.setWifiEnabled(true));
        assertTrue("Wifi must be configured to connect to an access point for this test.",
                receiver.waitForStateChange());

        mContext.unregisterReceiver(receiver);
    }/*from w  w  w .j av  a  2 s  . c  om*/
}

From source file:com.samknows.measurement.environment.NetworkData.java

public List<String> convert() {
    List<String> list = new ArrayList<String>();

    DCSStringBuilder builder = new DCSStringBuilder();
    builder.append(ID_PHONE);//from   w w w .j av  a2s .c o  m
    builder.append(time / 1000);
    builder.append(DCSConvertorUtil.convertPhoneType(phoneType));
    builder.append(DCSConvertorUtil.convertNetworkType(networkType));

    String s = "NONE";
    if (activeNetworkInfo != null) {
        switch (activeNetworkInfo.getType()) {
        case ConnectivityManager.TYPE_MOBILE: {
            s = "MOBILE";
            break;
        }
        case ConnectivityManager.TYPE_WIFI: {
            s = "WiFi";
            break;
        }
        }
    }
    builder.append(s);
    builder.append(isConnected ? 1 : 0);
    builder.append(isRoaming ? 1 : 0);
    list.add(builder.build());

    builder = new DCSStringBuilder();
    builder.append(ID_NETWORK_OP);
    builder.append(time / 1000);
    builder.append(networkOperatorCode);
    builder.append(networkOperatorName);
    list.add(builder.build());

    builder = new DCSStringBuilder();
    builder.append(ID_SIM_OP);
    builder.append(time / 1000);
    builder.append(simOperatorCode);
    builder.append(simOperatorName);
    list.add(builder.build());

    return list;
}

From source file:fr.inria.ucn.collectors.NetworkStateCollector.java

/**
 * /* w  ww.  java  2s  .  c  om*/
 * @param c
 * @param ts
 * @param change
 */
@SuppressWarnings("deprecation")
@SuppressLint({ "DefaultLocale", "NewApi" })
public void run(Context c, long ts, boolean change) {
    try {
        // current active interface (wifi or mobile) and config
        ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();

        JSONObject data = new JSONObject();
        data.put("on_network_state_change", change); // this collection run was triggered by network change
        data.put("is_connected", (ni != null && ni.isConnectedOrConnecting()));
        data.put("is_roaming", tm.isNetworkRoaming());

        // airplane mode ?
        if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            data.put("is_airplane_mode",
                    (Settings.System.getInt(c.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0));
        } else {
            data.put("is_airplane_mode",
                    (Settings.Global.getInt(c.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0));
        }

        if (ni != null) {
            JSONObject active = new JSONObject();
            active.put("type", ni.getType());
            active.put("subtype", ni.getSubtype());
            active.put("type_name", ni.getTypeName());
            active.put("subtype_name", ni.getSubtypeName());
            active.put("state", ni.getState().toString());
            active.put("detailed_state", ni.getDetailedState().toString());
            active.put("is_wifi", (ni.getType() == ConnectivityManager.TYPE_WIFI));
            data.put("active_network", active);

            if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
                data.put("wifi_network", getWifi(c));
            }
        }

        // mobile network details
        data.put("mobile_network", getMobile(tm));

        // kernel network statistics
        data.put("netstat", getNetstat());

        // interfaces config
        Map<String, JSONObject> stats = networkStats();
        data.put("ifconfig", getIfconfig(stats));

        // double check interfaces
        data.put("ip_addr_show", getIpAddr(stats));

        Helpers.sendResultObj(c, "network_state", ts, data);

    } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object", jex);
    }
}

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  . j  av a 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:hobby.wei.c.phone.Network.java

private static Type getType(NetworkInfo netInfo) {
    Type type;/*ww w  . ja  v a  2 s . c  om*/
    //?TYPE_WIFI?TYPE_MOBILE?TYPE_MOBILE_MMS
    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) { //wifi?
        type = Type.WIFI;
    } else { //TYPE_WIFI
        switch (netInfo.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            type = Type.NO_NET;
            break;
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
            type = Type.G2;
            break;
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_CDMA: //3G
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_1xRTT: //2.5GCDMA
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_IDEN:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = Type.G3;
            break;
        case TelephonyManager.NETWORK_TYPE_LTE:
            type = Type.G4;
            break;
        default:
            type = Type.G4;
            break;
        }
    }
    return type;
}

From source file:com.licubeclub.zionhs.Schedule.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule);

    final TextView MonthTxt = (TextView) findViewById(R.id.month);
    Button Minus = (Button) findViewById(R.id.minus);
    Button Plus = (Button) findViewById(R.id.plus);

    Calendar Cal = Calendar.getInstance();
    month = Cal.get(Calendar.MONTH) + 1;
    year = Cal.get(Calendar.YEAR);

    final int MONTH = month;
    final int YEAR = year;

    MonthTxt.setText(String.valueOf(year) + "." + String.valueOf(month));
    Minus.setOnClickListener(new View.OnClickListener() {
        @Override/*from   w  w  w  .ja v  a  2 s .c o m*/
        public void onClick(View v) {
            movement--;
            NewURL = "http://www.zion.hs.kr/main.php?menugrp=020500&master=diary&act=list&master_sid=1&"
                    + "SearchYear=" + YEAR + "&SearchMonth=" + MONTH + "&SearchCategory=&SearchMoveMonth="
                    + movement;
            URL = NewURL;
            networkTask();
            if (month == 1) {
                month = 12;
                year--;
            } else {
                month--;
            }
            MonthTxt.setText(String.valueOf(year) + "." + String.valueOf(month));
        }
    });
    Plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            movement++;
            NewURL = "http://www.zion.hs.kr/main.php?menugrp=020500&master=diary&act=list&master_sid=1&"
                    + "SearchYear=" + YEAR + "&SearchMonth=" + MONTH + "&SearchCategory=&SearchMoveMonth="
                    + movement;
            URL = NewURL;
            networkTask();
            if (month == 12) {
                month = 1;
                year++;
            } else {
                month++;
            }
            MonthTxt.setText(String.valueOf(year) + "." + String.valueOf(month));
        }
    });

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    listview = (ListView) findViewById(R.id.listView);

    cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    mobile = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    wifi = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    SRL = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    SRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            networkTask();
        }
    });

    networkTask();

}

From source file:com.tarun.smartwomen.WebViewDemoActivity.java

@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override/*from w  ww  . j  a v a  2  s  . co  m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainn);

    Intent intent = getIntent();
    reg = intent.getBooleanExtra("reg", false);

    i = (ImageView) findViewById(R.id.hj);

    try {

        big = new GifAnimationDrawable(getResources().openRawResource(R.raw.anim2));
        // big.setOneShot(true);
        android.util.Log.v("GifAnimationDrawable", "===>Four");
    } catch (IOException ioe) {

    }

    i.setImageDrawable(big);
    big.setVisible(true, true);

    sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    // historyStack = new LinkedList<Link>();
    webview = (WebView) findViewById(R.id.webkit);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDomStorageEnabled(true);
    // webview.addJavascriptInterface(new WebViewDemoActivity(), "Android");
    // webview.getSettings().setJavaScriptEnabled(true);

    webview.getSettings().setBuiltInZoomControls(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        // webview.getSettings().setPluginState(PluginState.ON);
        // webview.getSettings().setJavaScriptEnabled(true);
    } else {
        // IMPORTANT!! this method is no longer available since Android 4.3
        // so the code doesn't compile anymore
        // webview.getSettings().setPluginsEnabled(true);
    }

    // Internet

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    connected = false;
    if ((null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) && connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)
            || (null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) && connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {
        // we are connected to a network
        connected = true;
    }

    if (connected == false) {
        /*
         * Toast.makeText(Rss.this,
         * "Connect to internet and Restart Application",
         * Toast.LENGTH_SHORT).show();
         */
        webview.setVisibility(View.INVISIBLE);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(WebViewDemoActivity.this);

        // alertDialogBuilder.setTitle("Please connect to Internet");
        alertDialogBuilder.setMessage(
                "In order to provide the freshest recipes and juicing information this app must be connected to the internet, please check your internet settings");
        // set positive button: Yes message

        alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // cancel the alert box and put a Toast to the user

                startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show alert
        alertDialog.show();

    }

    // downloads
    // webview.setDownloadListener(new CustomDownloadListener());

    webview.setWebViewClient(new CustomWebViewClient());

    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {

        }

    });

    // http://stackoverflow.com/questions/2083909/android-webview-refusing-user-input
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
            }
            return false;
        }

    });

    if (reg == true) {
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl("http://om-msmartwoman.com/member/register");
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl("http://om-msmartwoman.com/member/register");
        }

        webview.requestFocus();
    } else {
        uhdj = sp.getString("your_int_key", "0");

        Log.e("Url is here ..............................", uhdj);
        // Welcome page loaded from assets directory
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl(uhdj);
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl(uhdj);
        }

        webview.requestFocus();
    }

}

From source file:com.alex.smartwomanmiddleeastfem.WebViewDemoActivity.java

@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override//from   w ww .ja  v a2  s.c  om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainn);

    Intent intent = getIntent();
    reg = intent.getBooleanExtra("reg", false);

    i = (ImageView) findViewById(R.id.hj);

    try {

        big = new GifAnimationDrawable(getResources().openRawResource(R.raw.anim2));
        // big.setOneShot(true);
        android.util.Log.v("GifAnimationDrawable", "===>Four");
    } catch (IOException ioe) {

    }

    i.setImageDrawable(big);
    big.setVisible(true, true);

    sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    // historyStack = new LinkedList<Link>();
    webview = (WebView) findViewById(R.id.webkit);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDomStorageEnabled(true);
    // webview.addJavascriptInterface(new WebViewDemoActivity(), "Android");
    // webview.getSettings().setJavaScriptEnabled(true);

    webview.getSettings().setBuiltInZoomControls(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        // webview.getSettings().setPluginState(PluginState.ON);
        // webview.getSettings().setJavaScriptEnabled(true);
    } else {
        // IMPORTANT!! this method is no longer available since Android 4.3
        // so the code doesn't compile anymore
        // webview.getSettings().setPluginsEnabled(true);
    }

    // Internet

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    connected = false;
    if ((null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) && connectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)
            || (null != connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) && connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {
        // we are connected to a network
        connected = true;
    }

    if (connected == false) {
        /*
         * Toast.makeText(Rss.this,
         * "Connect to internet and Restart Application",
         * Toast.LENGTH_SHORT).show();
         */
        webview.setVisibility(View.INVISIBLE);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(WebViewDemoActivity.this);

        // alertDialogBuilder.setTitle("Please connect to Internet");
        alertDialogBuilder.setMessage(
                "Through our app offers some offline features, in order to stay actively connected to the SmartWoman community in realtime, you need an internet connection, tap here to check your settings or wait until you have connection.");
        // set positive button: Yes message

        alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // cancel the alert box and put a Toast to the user

                startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show alert
        alertDialog.show();

    }

    // downloads
    // webview.setDownloadListener(new CustomDownloadListener());

    webview.setWebViewClient(new CustomWebViewClient());

    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {

        }

    });

    // http://stackoverflow.com/questions/2083909/android-webview-refusing-user-input
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
            }
            return false;
        }

    });

    if (reg == true) {
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl("http://www.qa-msmartwoman.com/member/register");
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl("http://www.qa-msmartwoman.com/member/register");
        }

        webview.requestFocus();
    } else {
        uhdj = sp.getString("your_int_key", "0");

        //   Log.e("Url is here ..............................", uhdj);
        // Welcome page loaded from assets directory
        if (Locale.getDefault().getLanguage().equals("es")) {

            webview.loadUrl(uhdj);
            // webview.loadUrl("http://live-juice-guru.gotpantheon.com/user/login");
        } else {
            webview.loadUrl(uhdj);
        }

        webview.requestFocus();
    }

}

From source file:com.github.czy1121.update.app.utils.UpdateUtil.java

public static boolean checkWifi(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    }/*from   w  w  w  . j  a  v  a 2 s . c o  m*/
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI;
}