|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Piotr Wittchen |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.pwittchen.networkevents.library; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.net.ConnectivityManager; |
| 20 | +import android.net.NetworkInfo; |
| 21 | + |
| 22 | +public final class NetworkHelper { |
| 23 | + /** |
| 24 | + * Helper method, which checks if device is connected to WiFi or mobile network. |
| 25 | + * @param context Activity or application context |
| 26 | + * @return boolean true if is connected to mobile or WiFi network. |
| 27 | + */ |
| 28 | + public static boolean isConnectedToWiFiOrMobileNetwork(Context context) { |
| 29 | + final String service = Context.CONNECTIVITY_SERVICE; |
| 30 | + final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service); |
| 31 | + final NetworkInfo networkInfo = manager.getActiveNetworkInfo(); |
| 32 | + |
| 33 | + if (networkInfo == null) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + final boolean isWifiNetwork = networkInfo.getType() == ConnectivityManager.TYPE_WIFI; |
| 38 | + final boolean isMobileNetwork = networkInfo.getType() == ConnectivityManager.TYPE_MOBILE; |
| 39 | + |
| 40 | + if (isWifiNetwork || isMobileNetwork) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | + } |
| 46 | +} |
0 commit comments