Android is device connected to the internet method - android

I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!

With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute

try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}

Related

Difference between `isConnected()` and `isAvailable()` in android `NetworkInfo`

I'm trying to check if the device is connected to internet or not. I have the below implementation to do that
public static boolean isConnectedToNetwork(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
NetworkInfo provides two methods isConnected() and isAvailable(). Which one should I use and what is the difference between them.
And is there a way to detect the state where the device is connected to Wifi without an internet connection?
If the device is connected to a network, isConnected returns true.
If the device is not connected but network is available to connect, isAvailable returns true, isConnected returns false.
You can read this topic for find your last question.
Android Check if there is WiFi but no internet
isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
- Always call this before attempting to perform data transactions.
isAvailable()
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include
- The device is out of the coverage area for any network of this type.
- The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
- The device's radio is turned off, e.g., because airplane mode is enabled.
Reference Link

Which API decides if the call is going through VoLTE or VoWiFi in Android?

I want to write an xposed module where I can redirect VoLTE calls through VoWiFi. I want to know which method decides if the call is going through VoLTE or VoWifi and I would hook that method and get the work done.
Basically using WiFi I want to give the network an illusion that the device is using mobile data and send calls through wifi
Note : I am new to android programming. Excuse me if my question looks vague.
So you want to give illusion that you are connected to WIFI even though you are on mobile data.
the way we check that is this:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
So what you can do is, hook getType method of ConnectivityManager and in afterhook method always return ConnectivityManager.TYPE_WIFI
the way to do that is by calling
param.setResult(ConnectivityManager.TYPE_WIFI);//may be you need casting here
in afterHook.
read more about connectivity here:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
Edit: I hope you are familiar with afterHook and beforeHook methods and how xposed works.

Detect if an android device has mobile data capability

I want to detect whether or not a device has mobile data capability. By mobile data capability I don't mean an active or connected mobile data connection, just the ability to use mobile data.
I currently use the following
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
//I assume device has mobile data capability
}
This works fine for most cases but one - If the device does not have calling facility but has the capability to utilize a cellular network for data connection only. Such devices are typically tablets which have a SIM card slot but it can be used only for data connection, not calling.
How can I detect whether the device has mobile data capability in this case and in all other cases? What is the best method?
I found the solution myself. Posting it for others who might need it.
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (ni == null) {
// Device does not have mobile data capability
}
From the documentation of getNetworkInfo(int networkType):
Parameters: networkType integer specifying which networkType in which you're interested.
Returns: a NetworkInfo object for the requested network type or null
if the type is not supported by the device. This method requires the
caller to hold the permission
android.Manifest.permission.ACCESS_NETWORK_STATE.
This method can be extended to check for other types of network as well. Just put the required networkType in getNetworkInfo().
Personally, I perform this test only if
getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
returns false. This check can then confirm whether or not device has mobile data capability even though it does not have telephony feature (which is the case with some tablets). If device has FEATURE_TELEPHONY, I assume it has mobile data capability. This way we can possibly reduce execution time in most cases.
hmm I have not tested this yet but if it has a data connection then its location over network probably is enabled, thus you can use FEATURE_LOCATION_NETWORK
Use ConnectivityManager and NetworkInfo to check the current state of connection:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo eventInfo = cm.getActiveNetworkInfo();
//first check to see if an active connection exists
if (eventInfo != null
&& eventInfo.getState() == NetworkInfo.State.CONNECTED) {
//now checks for the type of connection
switch(eventInfo.getType()) {
case ConnectivityManager.TYPE_MOBILE:
case ConnectivityManager.TYPE_MOBILE_DUN:
case ConnectivityManager.TYPE_MOBILE_HIPRI:
case ConnectivityManager.TYPE_MOBILE_MMS:
case ConnectivityManager.TYPE_MOBILE_SUPL:
//is mobile connection
break;
}
} else {
//device currently in a disconnected state, including connecting
}
*psudo code, may not 100% work on copy & paste

How can I check if Internet connection is turned on every n seconds?

Hi guys I'm new on android and I wish to check every "some" seconds if the internet connection has been turned on from the user.
This is my situation: I've a map on a fragment and I download from my app an xml file with the position of markers, titles, addresses, and many other things. Now if the person that download my app don't have the internet turned on, I can't place any markers on the map. So a popup come out that request to activate an internet connection. But if the person activate it without refresh the main activity nothing happens.
So how can I do it? Which methods I have to use?
There's no need to schedule an update based on an Internet resource if you aren't connected to the Internet. The following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity.
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
For Determining and Monitoring the Connectivity Status Help Link

Android: HttpClient parameters - Connection and Socket Timeout

I could't find any helpful tutorials on internet nor the documentation on developers site.
In my application i am connecting to a Web Server using HttpPost, when there is no internet connection, but wifi is on it shows a white screen and after some 10-15 secs "UnknownHostException".
I caught this Exception and made toast like
Unable to connect, check your internet connection.
and close the Activity (or the Application, since i am using finish() on the 1st Activity).
When the wifi itself is off i get an instant toast like"
You need internet connection to use this Application
but the 1st case is irritating. Taking 10-15 secs time and then showing the toast.
So i used HttpParameters and added a 5 sec ConnectionTimeout parameter.
But the application works same as before(no effect of this parameters).
How can i track if i hit ConnectionTime(5 secs over). So that i can show a Toast like
Slow internet connection
moreover why is the internet connection check not working when wifi is on but no internet
this is what i check when my application is lauched:
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (isOnline(cm, this, SignUpActivity.this)){
//continue
}
public static boolean isOnline(ConnectivityManager cm, Context c, Activity a) {
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
Toast.makeText(c, "You need internet access to run this application",
Toast.LENGTH_SHORT).show();
a.finish();
return false;
}
am i only checking whether device's wifi is on. if so, how can i check whether i have internet connection, instead of just wifi
Thank You
As for the first question, try using SocketTimeout instead.
As for the second question, the line
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Will only get Wi-fi status (i.e. phone wifi antenna turned on) but not actual connectivity. The function returns immediately, so that if wifi is turned off you can toast out without checking connection further. But when wi-fi is turned on, you should go on and check your server's actual reachability, with something like
InetAddress.getByName(host).isReachable(timeOut)

Categories

Resources