当前位置: 首页>>代码示例>>Java>>正文


Java ConnectivityManager类代码示例

本文整理汇总了Java中android.net.ConnectivityManager的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager类的具体用法?Java ConnectivityManager怎么用?Java ConnectivityManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ConnectivityManager类属于android.net包,在下文中一共展示了ConnectivityManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: observeNetworkConnectivity

import android.net.ConnectivityManager; //导入依赖的package包/类
@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
    final String service = Context.CONNECTIVITY_SERVICE;
    final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service);
    networkCallback = createNetworkCallback(context);

    registerIdleReceiver(context);

    final NetworkRequest request =
            new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
                    .build();

    manager.registerNetworkCallback(request, networkCallback);

    return connectivitySubject.toFlowable(BackpressureStrategy.LATEST).doOnCancel(new Action() {
        @Override public void run() {
            tryToUnregisterCallback(manager);
            tryToUnregisterReceiver(context);
        }
    }).startWith(Connectivity.create(context)).distinctUntilChanged().toObservable();
}
 
开发者ID:datalink747,项目名称:Rx_java2_soussidev,代码行数:22,代码来源:MarshmallowNetworkObservingStrategy.java

示例2: onReceive

import android.net.ConnectivityManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    try {
        String action = intent.getAction();
        if(action == Intent.ACTION_BATTERY_CHANGED){
            handleBattery(intent);
        }else if(action == ConnectivityManager.CONNECTIVITY_ACTION){
            AppModel.getNetworkState();
        }else if(action == Intent.ACTION_SCREEN_ON){
            AppModel.sDevScreenOff = false;
            LogUtils.i("screen on");
        }else if(action == Intent.ACTION_SCREEN_OFF){
            AppModel.sDevScreenOff = true;
            LogUtils.i("screen off");
        }
    }catch (Exception e){
        LogUtils.e("onReceive error ", e);
    }

}
 
开发者ID:XndroidDev,项目名称:Xndroid,代码行数:21,代码来源:XndroidReceiver.java

示例3: getConnectedNetworks

import android.net.ConnectivityManager; //导入依赖的package包/类
private static List<NetworkInfo> getConnectedNetworks(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return null;
    }
    final List<NetworkInfo> list = new ArrayList<NetworkInfo>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        NetworkInfo[] networkInfoList = cm.getAllNetworkInfo();
        if (networkInfoList != null) {
            final int length = networkInfoList.length;
            for (int i = 0; i < length; i++) {
                if (networkInfoList[i].getState() == NetworkInfo.State.CONNECTED) {
                    list.add(networkInfoList[i]);
                }
            }
        }
    } else {
        final Network[] networks = cm.getAllNetworks();
        if (networks != null && networks.length > 0) {
            NetworkInfo info;
            for (Network network : networks) {
                info = cm.getNetworkInfo(network);
                if (info != null && info.getState() == NetworkInfo.State.CONNECTED) {
                    list.add(info);
                }
            }
        }
    }
    return list;
}
 
开发者ID:LightSun,项目名称:android-util2,代码行数:31,代码来源:NetworkCompatUtil.java

示例4: isNetworkAvailable

import android.net.ConnectivityManager; //导入依赖的package包/类
public static boolean isNetworkAvailable(final Context context) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    boolean state = activeNetworkInfo != null && activeNetworkInfo.isConnected();
    if (!state) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(context.getString(R.string.internet_alert))
                .setCancelable(false)
                .setPositiveButton(context.getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }

    return state;

}
 
开发者ID:fekracomputers,项目名称:MuslimMateAndroid,代码行数:22,代码来源:Validations.java

示例5: isNetworkAvailable

import android.net.ConnectivityManager; //导入依赖的package包/类
public boolean isNetworkAvailable() {

        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
	}
 
开发者ID:Louis19910615,项目名称:youkes_browser,代码行数:19,代码来源:MainApp.java

示例6: haveNetworkConnection

import android.net.ConnectivityManager; //导入依赖的package包/类
private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
 
开发者ID:de-crypto,项目名称:ChatShala,代码行数:17,代码来源:MainActivity.java

示例7: getAvailableNetworkInfo

import android.net.ConnectivityManager; //导入依赖的package包/类
public static NetworkInfo getAvailableNetworkInfo(Context context) {
    NetworkInfo ni = null;
    try {
        ni = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (ni == null || !ni.isAvailable()) ? null : ni;
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:10,代码来源:Utils.java

示例8: checkPending

import android.net.ConnectivityManager; //导入依赖的package包/类
/**
 * Checks if there is already a pending request for the user.
 *
 * @param name of user pending
 * @return Boolean value. Whether if there is a pending request for the user.
 * @throws Exception
 */
private boolean checkPending(String name) { // change to more specific exception later.
    // Check if app is connected to a network.
    ConnectivityManager cm = (ConnectivityManager) getActivity().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null == activeNetwork) {
        Toast.makeText(getActivity(), "You are offline.", Toast.LENGTH_SHORT).show();
    } else {
        ElasticSearchUserController.GetUserTask getUserTask =
                new ElasticSearchUserController.GetUserTask();
        getUserTask.execute(name.toLowerCase());
        try {
            User user2 = getUserTask.get().get(0); // get first user from result
            String currentUsername = CurrentUserSingleton.getInstance().getUser().getName();
            return user2.getPending().contains(currentUsername);
        } catch (Exception e) {
            Log.d("Error", "Unable to get user from elastic search");
        }
    }
    return false;
}
 
开发者ID:CMPUT301W17T08,项目名称:Moodr,代码行数:29,代码来源:StrangerProfile.java

示例9: isTunnelNeeded

import android.net.ConnectivityManager; //导入依赖的package包/类
private boolean isTunnelNeeded(NetworkInfo info) {
	if (info == null) {
		Log.i("No connectivity: tunnel should be disabled");
		return false;
	}

	String pref = mPrefs.getTunnelMode();

	if (getString(R.string.tunnel_mode_entry_value_always).equals(pref)) {
		return true;
	}

	if (info.getType() != ConnectivityManager.TYPE_WIFI
			&& getString(R.string.tunnel_mode_entry_value_3G_only).equals(pref)) {
		Log.i("need tunnel: 'no wifi' connection");
		return true;
	}

	return false;
}
 
开发者ID:treasure-lau,项目名称:Linphone4Android,代码行数:21,代码来源:LinphoneManager.java

示例10: getNetworkType

import android.net.ConnectivityManager; //导入依赖的package包/类
/**
 * 获取当前网络类型
 *
 * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
 */
public static int getNetworkType(Context context) {
	int netType = NetworkUtils.NET_TYPE_NOT;
	ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
	if (networkInfo == null) {
		return netType;
	}
	int networkInfoType = networkInfo.getType();
	if (networkInfoType == ConnectivityManager.TYPE_MOBILE) {
		String extraInfo = networkInfo.getExtraInfo();
		if (!StringUtils.isEmpty(extraInfo)) {
			if (extraInfo.toLowerCase(Locale.getDefault()).equals("cmnet")) {
				netType = NetworkUtils.NET_TYPE_CM_NET;
			} else {
				netType = NetworkUtils.NET_TYPE_CM_WAP;
			}
		}
	} else if (networkInfoType == ConnectivityManager.TYPE_WIFI) {
		netType = NetworkUtils.NET_TYPE_WIFI;
	}
	return netType;
}
 
开发者ID:PlutoArchitecture,项目名称:Pluto-Android,代码行数:28,代码来源:NetworkUtils.java

示例11: isConnected

import android.net.ConnectivityManager; //导入依赖的package包/类
/**
 * 判断网络是否连接
 *
 * @param context context
 * @return true is connected
 */
@Deprecated
public static boolean isConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null != connectivity) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:shenhuanet,项目名称:AndroidOpen,代码行数:20,代码来源:NetworkUtils.java

示例12: registerLollipopNetworkReceiver

import android.net.ConnectivityManager; //导入依赖的package包/类
public static void registerLollipopNetworkReceiver(Context context) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.registerNetworkCallback(
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                        .build(),
                new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        boolean connected = false;
                        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            connected = cm.bindProcessToNetwork(network);
                        } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                            //noinspection deprecation
                            connected = ConnectivityManager.setProcessDefaultNetwork(network);
                        }
                        if(connected) {
                            Customerly.get().__SOCKET__check();
                        }
                    }
                });
    }
}
 
开发者ID:customerly,项目名称:Customerly-Android-SDK,代码行数:26,代码来源:IU_NetworkReceiver.java

示例13: testNetworkChangeNotifierIsOnline

import android.net.ConnectivityManager; //导入依赖的package包/类
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testNetworkChangeNotifierIsOnline() throws InterruptedException {
    Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
    // For any connection type it should return true.
    for (int i = ConnectivityManager.TYPE_MOBILE; i < ConnectivityManager.TYPE_VPN; i++) {
        mConnectivityDelegate.setActiveNetworkExists(true);
        mConnectivityDelegate.setNetworkType(i);
        mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
        assertTrue(NetworkChangeNotifier.isOnline());
    }
    mConnectivityDelegate.setActiveNetworkExists(false);
    mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
    assertFalse(NetworkChangeNotifier.isOnline());
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:17,代码来源:NetworkChangeNotifierTest.java

示例14: isInternetOn

import android.net.ConnectivityManager; //导入依赖的package包/类
public boolean isInternetOn () {

//                Log.e(TAG, "isInternetOn fired");
                ConnectivityManager connec =
                        (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

                // Check for network connections
                if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
                        connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
                        connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
                        connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {

                        // if connected with internet
                        return true;

                } else if (
                        connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||
                                connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {

                        return false;
                }
                return  false;

        }
 
开发者ID:kuldeep725,项目名称:GogoNew,代码行数:25,代码来源:MapsActivity.java

示例15: loadnet

import android.net.ConnectivityManager; //导入依赖的package包/类
private void loadnet() {


        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE);
        //获取系统的连接服务。
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        //获取网络的连接情况。
        if (networkInfo != null && networkInfo.isAvailable()) {
          /*  if (networkInfo.getType()==connectivityManager.TYPE_WIFI){
                Toast.makeText(A.this,"网络已启动啦(WIFI)",Toast.LENGTH_SHORT).show();
            }else if (networkInfo.getType()==connectivityManager.TYPE_MOBILE) {
                Toast.makeText(A.this,"网络已启动啦(3G)",Toast.LENGTH_SHORT).show();

            }*/
            loadingLayout.setStatus(LoadingLayout.Success);//加载成功


        } else {
            loadingLayout.setStatus(LoadingLayout.Loading);
        }

    }
 
开发者ID:Shmilyz,项目名称:Swap,代码行数:23,代码来源:SpecialFragment.java


注:本文中的android.net.ConnectivityManager类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。