This method checks whether mobile is connected to internet and returns true if connected:
private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected(); }
in manifest,
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Edit: This method actually checks if device is connected to internet(There is a possibility it’s connected to a network but not to internet).
public boolean isInternetAvailable() { try { InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name return !ipAddr.equals(""); } catch (Exception e) { return false; } }
Related Posts:
- What does localhost:8080 mean?
- Is it possible to repeat or redistribute a Bluetooth signal? [closed]
- java.net.ConnectException: Connection refused
- How can you find out which process is listening on a TCP or UDP port on Windows?
- java.net.ConnectException :connection timed out: connect?
- What is the difference between a port and a socket?
- Does TCP send a SYN/ACK on every packet or only on the first connection?
- How can I fix ‘android.os.NetworkOnMainThreadException’?
- Finding local IP addresses using Python’s stdlib
- Docker-compose container using host DNS server
- UDP vs TCP, how much faster is it?
- How does IPv4 Subnetting Work?
- I’ve inherited a rat’s nest of cabling. What now?
- What is the difference between Unix sockets and TCP/IP sockets?
- Why couldn’t MAC addresses be used instead of IPv4|6 for networking? [closed]
- VirtualBox: How to set up networking so both host and guest can access internet and talk to each other
- How do VLANs work?
- What causes the ‘Connection Refused’ message?
- How does IPv6 subnetting work and how does it differ from IPv4 subnetting?
- Why do consoles sometimes hang forever when SSH connection breaks?
- Switching to IPv6 implies dropping NAT. Is that a good thing?
- How much network latency is “typical” for east – west coast USA?
- How do I find if there is a rogue DHCP server on my Network?
- Is X.Y.Z.0 a valid IP address?
- ssh tunnel refusing connections with “channel 2: open failed”
- How do I make a connection private on Windows Server 2012 R2
- Should network hardware be set to “autonegotiate” speeds or fixed speeds?
- What is “anycast” and how is it helpful?
- What happens when you plug two sides of a cable to a single networking device?
- How to reload default Mac OSX routing table without rebooting
- how to set the background color of the status bar during the launching phase [duplicate]
- How to show a loading gif while an APi is being called in xamarin android?
- 1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]
- Dial pad to get phone number (with Android button images)
- ping response “Request timed out.” vs “Destination Host unreachable”
- Emulator: ERROR: x86 emulation currently requires hardware acceleration
- Emulator: ERROR: x86 emulation currently requires hardware acceleration
- ‘App not Installed’ Error on Android
- ‘App not Installed’ Error on Android
- Draw a transparent circle onto a filled android canvas
- How to use navigateUp to close the application
- An existing connection was forcibly closed by the remote host
- “unable to locate adb” using Android Studio [duplicate]
- “unable to locate adb” using Android Studio [duplicate]
- Can’t start Eclipse – Java was started but returned exit code=13
- Can’t start Eclipse – Java was started but returned exit code=13
- What exactly is “com.google.android.packageinstaller”
- Differences between arm64 and aarch64
- Converting double to string
- Chrome hangs after certain amount of data transfered – waiting for available socket
- Android WebView err_unknown_url_scheme
- google console error `OR-IEH-01`
- Install an apk file from command prompt?
- How to decompile an APK or DEX file on Android platform? [closed]
- Install an apk file from command prompt?
- Android Studio Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
- Error parsing data org.json.JSONException: Value
- What does “|=” mean? (pipe equal operator)
- My users get “net::err_name_not_resolved”
- Error – Android resource linking failed (AAPT2 27.0.3 Daemon #0)
- Solving “adb server version doesn’t match this client” error [duplicate]
- Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm
- Unity remote 5 not working
- Error – Android resource linking failed (AAPT2 27.0.3 Daemon #0)
- Solving “adb server version doesn’t match this client” error [duplicate]
- Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm
- How to display Toast in Android?
- What is Android’s file system?
- How to use adb command to push a file on device without sd card
- Can’t create handler inside thread that has not called Looper.prepare()
- What is a StringIndexOutOfBoundsException? How can I fix it?
- recyclerview No adapter attached; skipping layout
- Exception : AAPT2 error: check logs for details
- This adb server’s $ADB_VENDOR_KEYS is not set
- Can’t create handler inside thread that has not called Looper.prepare()
- This adb server’s $ADB_VENDOR_KEYS is not set
- ‘adb’ is not recognized as an internal or external command, operable program or batch file
- Connect: Socket operation on non-socket
- Connect: Socket operation on non-socket
- How do popBackStack() and replace() operations differ?
- E: unable to locate package pip
- recyclerview No adapter attached; skipping layout
- Exception : AAPT2 error: check logs for details
- Flutter pageview : Find first and last page and swipe directions
- What are the possible reasons for NFC Service to become dead after some time?
- How can I find and run the keytool
- Android ADB devices unauthorized
- adb is not recognized as internal or external command, operable program or batch file
- Enable VT-x in your BIOS security settings (refer to documentation for your computer)
- NetworkType.UNMETERED vs NetworkType.METERED – PeriodicWork
- @ converted to %40 in HTTPPost request
- Getting net::ERR_UNKNOWN_URL_SCHEME while calling telephone number from HTML page in Android
- Simple Android RecyclerView example
- Find all Bluetooth devices (headsets, phones etc) nearby, without forcing the devices in discoverable mode
- You need to use a Theme.AppCompat theme (or descendant) with this activity
- “Default Activity Not Found” on Android Studio upgrade
- You need to use a Theme.AppCompat theme (or descendant) with this activity
- Unfortunately MyApp has stopped. How can I solve this?
- Still getting warning : Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’
- What is ‘Context’ on Android?