Android examples for Network:Network Connection
Checks whether there is connection of the requested connectionType available or not.
/*//from w w w.j a va2 s . c om * ------------------------------------------------------------------------------------------------= * Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies] * ------------------------------------------------------------------------------------------------= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ------------------------------------------------------------------------------------------------= */ //package com.java2s; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; public class Main { /** * Checks whether there is connection of the requested <var>connectionType</var> available or not. * <p> * See {@link android.net.NetworkInfo#isAvailable()} for additional info. * * @param context Context used to access {@link android.net.ConnectivityManager}. * @param connectionType Type of the desired connection of which availability to check. * @return {@code True} if connection of the requested type available, {@code false} * otherwise. * @see #isConnectionEstablished(android.content.Context, int) */ public static boolean isConnectionAvailable(@NonNull Context context, int connectionType) { final NetworkInfo info = accessManager(context).getNetworkInfo( connectionType); return info != null && info.isAvailable(); } /** * Accesses the ConnectivityManager service using the given <var>context</var>. * * @param context Context from which ConnectivityManager should be accessed. * @return An instance of ConnectivityManager. */ private static ConnectivityManager accessManager(Context context) { return (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); } }