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
- Dial pad to get phone number (with Android button images)
- Draw a transparent circle onto a filled android canvas
- Can’t start Eclipse – Java was started but returned exit code=13
- Android WebView err_unknown_url_scheme
- Install an apk file from command prompt?
- Unity remote 5 not working
- recyclerview No adapter attached; skipping layout
- How do popBackStack() and replace() operations differ?
- recyclerview No adapter attached; skipping layout
- How to install APK from PC?
- Error:(1, 0) Plugin with id ‘com.android.application’ not found
- ERROR Android emulator gets killed
- Android device does not show up in adb list
- Error “File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it”
- How to recompile with -Xlint:deprecation
- Attempt to invoke virtual method ‘…’ on a null object reference
- Getting java.net.SocketTimeoutException: Connection timed out in android
- How to convert int to Integer
- Facebook login NullPointerException
- “Failed to install the following Android SDK packages as some licences have not been accepted” error
- This view is not constrained
- How to use ScrollView in Android?
- How to parse JSON boolean value?
- What is com.google.android.gms.persistent and why is it always using the CPU?
- Parcelable encountered IOException writing serializable object getactivity()
- onActivityResult() not called
- “com.example is restricted” when uploading APK to Play Store
- How to print to the console in Android Studio?
- Android “Only the original thread that created a view hierarchy can touch its views.”
- How do we use runOnUiThread in Android?
- Extending from two classes
- Error getGoogleAppId failed with status: 10 Android KitKat – GCM and Google Drive
- Unity3D. Trying to send command for object without authority
- How to add .gif Animation in unity Scene ? Does Unity support Animated GIFS?
- Rotating a view in Android
- Android error “unable to find explicit activity class”
- Can not resolve method ‘findViewById(int)’
- Cannot resolve symbol ‘context’
- How to create JSON Object using String?
- Why doesn’t RecyclerView have onItemClickListener()?
- How to get current time and date in Android
- Gradle DSL method not found: ‘compile()’
- how to use getSharedPreferences in android
- Set transparent background of an imageview on Android
- Play sound on button click android
- How to make the corners of a button round?
- ADB error: cannot connect to daemon
- Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- ClassLoader referenced unknown path: /data/app/
- Unknown URL content://downloads/my_downloads
- android edittext onchange listener
- E/AndroidRuntime﹕ FATAL EXCEPTION: main
- How to authorize and accept ADB RSA key with broken touch screen on Android
- why android studio show error of “Missing constraints in constraintlayout”?
- How to make layout with rounded corners..?
- java.lang.ClassNotFoundException: org.postgresql.Driver, Android
- Android Fragment no view found for ID?
- How to disable action bar permanently
- Manifest Merger failed with multiple errors in Android Studio
- hardcoded string “row three”, should use @string resource
- E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7519c0
- How to handle .tar.md5 files
- Why am I getting Permission Denied when trying to push a Sqlite file to my rooted Android device?
- Scrollview can host only one direct child
- Android studio- “SDK tools directory is missing”
- How can I make a wp site in a mobile application?
- How to connect android app with WordPress website?
- Resolve host name from IP address
- How to determine the hostname from an IP address in a Windows network?
- How to check the physical status of an ethernet port in Linux?