Saya perlu memberi tahu apakah perangkat saya memiliki koneksi Internet atau tidak. Saya menemukan banyak jawaban seperti:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
(Diambil dari Detect apakah ada koneksi Internet yang tersedia di Android .)
Tetapi ini tidak benar, misalnya jika saya terhubung ke jaringan nirkabel yang tidak memiliki akses Internet , metode ini akan mengembalikan true… Apakah ada cara untuk mengetahui apakah perangkat memiliki koneksi Internet dan tidak jika hanya terhubung untuk sesuatu?
android
internet-connection
android-internet
Buda Gavril
sumber
sumber
isNetworkConnected
blok fungsi berisi kode usang untuk semuaBuild.VERSION.SDK_INT >= Build.VERSION_CODES.M
. Diskusi yang lebih mendalam tentang masalah ini & cara memperbaikinya dapat ditemukan di sini stackoverflow.com/questions/53532406/…Jawaban:
Kamu benar. Kode yang Anda berikan hanya memeriksa apakah ada koneksi jaringan. Cara terbaik untuk memeriksa apakah ada koneksi Internet yang aktif adalah dengan mencoba dan menyambung ke server yang dikenal melalui http.
public static boolean hasActiveInternetConnection(Context context) { if (isNetworkAvailable(context)) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection()); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 200); } catch (IOException e) { Log.e(LOG_TAG, "Error checking internet connection", e); } } else { Log.d(LOG_TAG, "No network available!"); } return false; }
Tentu saja Anda dapat mengganti
http://www.google.com
URL untuk server lain yang ingin Anda sambungkan, atau server yang Anda tahu memiliki waktu aktif yang baik.Seperti yang ditunjukkan Tony Cho dalam komentar di bawah ini , pastikan Anda tidak menjalankan kode ini di utas utama, jika tidak, Anda akan mendapatkan pengecualian NetworkOnMainThread (di Android 3.0 atau lebih baru). Gunakan AsyncTask atau Runnable sebagai gantinya.
Jika Anda ingin menggunakan google.com, Anda harus melihat modifikasi Jeshurun. Dalam jawabannya dia mengubah kode saya dan membuatnya sedikit lebih efisien. Jika Anda terhubung ke
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204") .openConnection());
dan kemudian periksa kode respons untuk 204
return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0);
maka Anda tidak perlu mengambil seluruh beranda google terlebih dahulu.
sumber
ConnectivityManager
tidak memerlukan ini). Jika tidak, pada perangkat Android 4.x +, Anda akan mendapatkanNetworkOnMainThread
pengecualian.Saya telah sedikit mengubah jawaban THelper, untuk menggunakan peretasan yang diketahui yang sudah digunakan Android untuk memeriksa apakah jaringan WiFi yang terhubung memiliki akses Internet. Ini jauh lebih efisien daripada mengambil seluruh halaman beranda Google. Lihat di sini dan di sini untuk info lebih lanjut.
public static boolean hasInternetAccess(Context context) { if (isNetworkAvailable(context)) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204") .openConnection()); urlc.setRequestProperty("User-Agent", "Android"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0); } catch (IOException e) { Log.e(TAG, "Error checking internet connection", e); } } else { Log.d(TAG, "No network available!"); } return false; }
sumber
public boolean isInternetWorking() { boolean success = false; try { URL url = new URL("https://google.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(10000); connection.connect(); success = connection.getResponseCode() == 200; } catch (IOException e) { e.printStackTrace(); } return success; }
mengembalikan true jika internet benar-benar tersedia
Pastikan Anda memiliki dua izin ini
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
jika http tidak berfungsi karena keamanan android baru mereka tidak mengizinkan komunikasi teks biasa sekarang. untuk saat ini hanya untuk melewatkannya.
sumber
Jika Anda menargetkan Lollipop atau lebih tinggi, dimungkinkan untuk menggunakan kelas NetworkCapabilities baru, yaitu:
public static boolean hasInternetConnection(final Context context) { final ConnectivityManager connectivityManager = (ConnectivityManager)context. getSystemService(Context.CONNECTIVITY_SERVICE); final Network network = connectivityManager.getActiveNetwork(); final NetworkCapabilities capabilities = connectivityManager .getNetworkCapabilities(network); return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); }
sumber
connectivityManager.getActiveNetwork();
membutuhkan API lvl23 yang merupakan Marshmallow dan bukan Lollipop.capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
sepertinya melakukan trik untukku!Anda tidak perlu membuat koneksi HTTP penuh. Anda dapat mencoba membuka koneksi TCP ke host yang dikenal dan jika berhasil, Anda memiliki konektivitas internet.
public boolean hostAvailable(String host, int port) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(host, port), 2000); return true; } catch (IOException e) { // Either we have a timeout or unreachable host or failed DNS lookup System.out.println(e); return false; } }
Kemudian periksa dengan:
boolean online = hostAvailable("www.google.com", 80);
sumber
AsyncTask
Berdasarkan jawaban yang diterima, saya telah membangun kelas ini dengan pendengar sehingga Anda dapat menggunakannya di utas utama:
Pertama : kelas InterntCheck yang memeriksa koneksi internet di latar belakang kemudian memanggil metode pendengar dengan hasilnya.
public class InternetCheck extends AsyncTask<Void, Void, Void> { private Activity activity; private InternetCheckListener listener; public InternetCheck(Activity x){ activity= x; } @Override protected Void doInBackground(Void... params) { boolean b = hasInternetAccess(); listener.onComplete(b); return null; } public void isInternetConnectionAvailable(InternetCheckListener x){ listener=x; execute(); } private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null; } private boolean hasInternetAccess() { if (isNetworkAvailable()) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204").openConnection()); urlc.setRequestProperty("User-Agent", "Android"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0); } catch (IOException e) { e.printStackTrace(); } } else { Log.d("TAG", "No network available!"); } return false; } public interface InternetCheckListener{ void onComplete(boolean connected); } }
Kedua : buat instance kelas di utas utama dan tunggu responsnya (jika Anda telah bekerja dengan api Firebase untuk android sebelumnya pasti sudah tidak asing lagi bagi Anda!).
new InternetCheck(activity).isInternetConnectionAvailable(new InternetCheck.InternetCheckListener() { @Override public void onComplete(boolean connected) { //proceed! } });
Sekarang di dalam metode onComplete Anda akan mendapatkan apakah perangkat terhubung ke internet atau tidak.
sumber
coba yang ini
public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context) { this._context = context; } public boolean isConnectingToInternet() { if (networkConnectivity()) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL( "http://www.google.com").openConnection()); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(3000); urlc.setReadTimeout(4000); urlc.connect(); // networkcode2 = urlc.getResponseCode(); return (urlc.getResponseCode() == 200); } catch (IOException e) { return (false); } } else return false; } private boolean networkConnectivity() { ConnectivityManager cm = (ConnectivityManager) _context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; } }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
if((new ConnectionDetector(MyService.this)).isConnectingToInternet()){ Log.d("internet status","Internet Access"); }else{ Log.d("internet status","no Internet Access"); }
sumber
Anda dapat melakukan ini menggunakan
ConnectivityManager
API untuk android. Ini memungkinkan Anda untuk memeriksa apakah Anda terhubung ke internet dan jenis koneksi internet yang Anda sambungkan. Pada dasarnya, Metered atau Un-metered.Untuk memeriksa koneksi internet.
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
Tautan dokumentasi: https://developer.android.com/training/monitoring-device-state/connectivity-status-type
sumber
private static NetworkUtil mInstance; private volatile boolean mIsOnline; private NetworkUtil() { ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(new Runnable() { @Override public void run() { boolean reachable = false; try { Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com"); int returnVal = process.waitFor(); reachable = (returnVal==0); } catch (Exception e) { e.printStackTrace(); } mIsOnline = reachable; } }, 0, 5, TimeUnit.SECONDS); } public static NetworkUtil getInstance() { if (mInstance == null) { synchronized (NetworkUtil.class) { if (mInstance == null) { mInstance = new NetworkUtil(); } } } return mInstance; } public boolean isOnline() { return mIsOnline; }
Semoga kode di atas membantu Anda, juga pastikan Anda memiliki izin internet di aplikasi Anda.
sumber
Cara terbaru untuk melakukannya dari dokumentasi adalah dengan menggunakan
ConnectivityManager
kueri jaringan aktif dan menentukan apakah memiliki konektivitas Internet.public boolean hasInternetConnectivity() { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return (activeNetwork != null && activeNetwork.isConnectedOrConnecting()); }
Tambahkan dua izin ini ke file AndroidManifest.xml Anda:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
sumber
Periksa jenis wifi di pengelola konektivitas:
//check network connection ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean hasNetworkConnection = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); System.out.println("Connection ? : " + hasNetworkConnection); //check wifi boolean hasWifiConnection = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI; System.out.println("Wifi ? : " + hasWifiConnection);
Dokumentasi Android menjelaskan 'TYPE_WIFI' sebagai 'Koneksi data WIFI. Perangkat mungkin mendukung lebih dari satu. '
sumber
Solusi bagus untuk memeriksa apakah jaringan aktif memiliki koneksi internet:
public boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { Network network = connectivityManager.getActiveNetwork(); NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); return networkCapabilities != null && networkCapabilities .hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); } return false; }
sumber
Anda dapat menggunakan ConnectivityManager.
val cm = getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val activeNetwork: NetworkInfo? = cm.activeNetworkInfo val dialogBuilder = AlertDialog.Builder(this) if (activeNetwork!=null) // Some network is available { if (activeNetwork.isConnected) { // Network is connected to internet }else{ // Network is NOT connected to internet }
Periksa ini dan ini
sumber
Gunakan kelas berikut , diperbarui ke level API terakhir: 29.
// License: MIT // http://opensource.org/licenses/MIT package net.i2p.android.router.util; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Build; import android.telephony.TelephonyManager; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.concurrent.CancellationException; /** * Check device's network connectivity and speed. * * @author emil http://stackoverflow.com/users/220710/emil * @author str4d * @author rodrigo https://stackoverflow.com/users/5520417/rodrigo */ public class ConnectivityAndInternetAccessCheck { private static ArrayList < String > hosts = new ArrayList < String > () { { add("google.com"); add("facebook.com"); add("apple.com"); add("amazon.com"); add("twitter.com"); add("linkedin.com"); add("microsoft.com"); } }; /** * Get the network info. * * @param context the Context. * @return the active NetworkInfo. */ private static NetworkInfo getNetworkInfo(Context context) { NetworkInfo networkInfo = null; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { networkInfo = cm.getActiveNetworkInfo(); } return networkInfo; } /** * Gets the info of all networks * @param context The context * @return an array of @code{{@link NetworkInfo}} */ private static NetworkInfo[] getAllNetworkInfo(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getAllNetworkInfo(); } /** * Gives the connectivity manager * @param context The context * @return the @code{{@link ConnectivityManager}} */ private static ConnectivityManager getConnectivityManager(Context context) { return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); } /** * Check if there is any connectivity at all. * * @param context the Context. * @return true if we are connected to a network, false otherwise. */ public static boolean isConnected(Context context) { boolean isConnected = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ConnectivityManager connectivityManager = ConnectivityAndInternetAccessCheck.getConnectivityManager(context); Network[] networks = connectivityManager.getAllNetworks(); networksloop: for (Network network: networks) { if (network == null) { isConnected = false; } else { NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)){ isConnected = true; break networksloop; } else { isConnected = false; } } } } else { NetworkInfo[] networkInfos = ConnectivityAndInternetAccessCheck.getAllNetworkInfo(context); networkinfosloop: for (NetworkInfo info: networkInfos) { // Works on emulator and devices. // Note the use of isAvailable() - without this, isConnected() can // return true when Wifi is disabled. // http://stackoverflow.com/a/2937915 isConnected = info != null && info.isAvailable() && info.isConnected(); if (isConnected) { break networkinfosloop; } } } return isConnected; } /** * Check if there is any connectivity to a Wifi network. * * @param context the Context. * @return true if we are connected to a Wifi network, false otherwise. */ public static boolean isConnectedWifi(Context context) { boolean isConnectedWifi = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ConnectivityManager connectivityManager = ConnectivityAndInternetAccessCheck.getConnectivityManager(context); Network[] networks = connectivityManager.getAllNetworks(); networksloop: for (Network network: networks) { if (network == null) { isConnectedWifi = false; } else { NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)){ if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { isConnectedWifi = true; break networksloop; } else { isConnectedWifi = false; } } } } } else { NetworkInfo[] networkInfos = ConnectivityAndInternetAccessCheck.getAllNetworkInfo(context); networkinfosloop: for (NetworkInfo n: networkInfos) { isConnectedWifi = n != null && n.isAvailable() && n.isConnected() && n.getType() == ConnectivityManager.TYPE_WIFI; if (isConnectedWifi) { break networkinfosloop; } } } return isConnectedWifi; } /** * Check if there is any connectivity to a mobile network. * * @param context the Context. * @return true if we are connected to a mobile network, false otherwise. */ public static boolean isConnectedMobile(Context context) { boolean isConnectedMobile = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ConnectivityManager connectivityManager = ConnectivityAndInternetAccessCheck.getConnectivityManager(context); Network[] allNetworks = connectivityManager.getAllNetworks(); networksloop: for (Network network: allNetworks) { if (network == null) { isConnectedMobile = false; } else { NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)){ if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { isConnectedMobile = true; break networksloop; } else { isConnectedMobile = false; } } } } } else { NetworkInfo[] networkInfos = ConnectivityAndInternetAccessCheck.getAllNetworkInfo(context); networkinfosloop: for (NetworkInfo networkInfo: networkInfos) { isConnectedMobile = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE; if (isConnectedMobile) { break networkinfosloop; } } } return isConnectedMobile; } /** * Check if there is fast connectivity. * * @param context the Context. * @return true if we have "fast" connectivity, false otherwise. */ public static boolean isConnectedFast(Context context) { boolean isConnectedFast = false; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { NetworkInfo[] networkInfos = ConnectivityAndInternetAccessCheck.getAllNetworkInfo(context); networkInfosloop: for (NetworkInfo networkInfo: networkInfos) { isConnectedFast = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() && isConnectionFast(networkInfo.getType(), networkInfo.getSubtype()); if (isConnectedFast) { break networkInfosloop; } } } else { throw new UnsupportedOperationException(); } return isConnectedFast; } /** * Check if the connection is fast. * * @param type the network type. * @param subType the network subtype. * @return true if the provided type/subtype combination is classified as fast. */ private static boolean isConnectionFast(int type, int subType) { if (type == ConnectivityManager.TYPE_WIFI) { return true; } else if (type == ConnectivityManager.TYPE_MOBILE) { switch (subType) { case TelephonyManager.NETWORK_TYPE_1xRTT: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_CDMA: return false; // ~ 14-64 kbps case TelephonyManager.NETWORK_TYPE_EDGE: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_EVDO_0: return true; // ~ 400-1000 kbps case TelephonyManager.NETWORK_TYPE_EVDO_A: return true; // ~ 600-1400 kbps case TelephonyManager.NETWORK_TYPE_GPRS: return false; // ~ 100 kbps case TelephonyManager.NETWORK_TYPE_HSDPA: return true; // ~ 2-14 Mbps case TelephonyManager.NETWORK_TYPE_HSPA: return true; // ~ 700-1700 kbps case TelephonyManager.NETWORK_TYPE_HSUPA: return true; // ~ 1-23 Mbps case TelephonyManager.NETWORK_TYPE_UMTS: return true; // ~ 400-7000 kbps /* * Above API level 7, make sure to set android:targetSdkVersion * to appropriate level to use these */ case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 return true; // ~ 1-2 Mbps case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 return true; // ~ 5 Mbps case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 return true; // ~ 10-20 Mbps case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 return false; // ~25 kbps case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 return true; // ~ 10+ Mbps // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: default: return false; } } else { return false; } } public ArrayList < String > getHosts() { return hosts; } public void setHosts(ArrayList < String > hosts) { this.hosts = hosts; } //TODO Debug on devices /** * Checks that Internet is available by pinging DNS servers. */ private static class InternetConnectionCheckAsync extends AsyncTask < Void, Void, Boolean > { private Context context; /** * Creates an instance of this class * @param context The context */ public InternetConnectionCheckAsync(Context context) { this.setContext(context); } /** * Cancels the activity if the device is not connected to a network. */ @Override protected void onPreExecute() { if (!ConnectivityAndInternetAccessCheck.isConnected(getContext())) { cancel(true); } } /** * Tells whether there is Internet access * @param voids The list of arguments * @return True if Internet can be accessed */ @Override protected Boolean doInBackground(Void...voids) { return isConnectedToInternet(getContext()); } @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); } /** * The context */ public Context getContext() { return context; } public void setContext(Context context) { this.context = context; } } //network calls shouldn't be called from main thread otherwise it will throw //NetworkOnMainThreadException /** * Tells whether Internet is reachable * @return true if Internet is reachable, false otherwise * @param context The context */ public static boolean isInternetReachable(Context context) { try { return new InternetConnectionCheckAsync(context).execute().get(); } catch (CancellationException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return false; } /** * Tells whether there is Internet connection * @param context The context * @return @code {true} if there is Internet connection */ private static boolean isConnectedToInternet(Context context) { boolean isAvailable = false; if (!ConnectivityAndInternetAccessCheck.isConnected(context)) { isAvailable = false; } else { try { foreachloop: for (String h: new ConnectivityAndInternetAccessCheck().getHosts()) { if (isHostAvailable(h)) { isAvailable = true; break foreachloop; } } } catch (IOException e) { e.printStackTrace(); } } return isAvailable; } /** * Checks if the host is available * @param hostName * @return * @throws IOException */ private static boolean isHostAvailable(String hostName) throws IOException { try (Socket socket = new Socket()) { int port = 80; InetSocketAddress socketAddress = new InetSocketAddress(hostName, port); socket.connect(socketAddress, 3000); return true; } catch (UnknownHostException unknownHost) { return false; } } }
sumber
Anda dapat melakukan ini dengan kelas yang sangat sederhana.
class CheckInternet { fun isNetworkAvailable(context: Context): Boolean { val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? val activeNetworkInfo = connectivityManager!!.activeNetworkInfo return activeNetworkInfo != null && activeNetworkInfo.isConnected } }
Sekarang Anda dapat memeriksanya dari kelas mana pun.
if (CheckInternet().isNetworkAvailable(this)) { //connected with internet }else{ //Not connected with internet }
sumber