美文网首页
android 检测网络状态 wifi | mobile | n

android 检测网络状态 wifi | mobile | n

作者: 冰风47 | 来源:发表于2016-06-03 00:42 被阅读113次
    private void checkNetworkConnection() {
        // BEGIN_INCLUDE(connect)
        ConnectivityManager connMgr =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
            mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
            if (wifiConnected) {
                Log.i(TAG, getString(R.string.wifi_connection));
            } else if (mobileConnected) {
                Log.i(TAG, getString(R.string.mobile_connection));
            }
        } else {
            Log.i(TAG, getString(R.string.no_wifi_or_mobile));
        }
        // END_INCLUDE(connect)
    }
    

    相关文章

      网友评论

          本文标题:android 检测网络状态 wifi | mobile | n

          本文链接:https://www.haomeiwen.com/subject/zjbkdttx.html