I am using some code to detect network state and it works fine but sometimes the device is connected to wi-fi network but actually there is no internet .. i mean it is connected to router but the line is not working .. for example, when telephone line is cut off .. something like that ..
now how can i detect that ??
This is my code :-
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
Related
In my android app I have to gather a lot of information regarding the cell tower
like Network name, Network code etc etc but before I start capturing all those
things I want to make sure that a cell tower connections exists
i.e. GSM or CDMA, but I want to ensure that there is a connection. Otherwise
I would just like to return.
For example, as we do it in case of Wi Fi. With the help of WifiManager's isWifiEnabled() method we ensure first hand whether we have Wifi running or not.
Similar to that, for Cell Tower.
As described in this post, you can find as below
boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
// True if the phone is connected to some type of network i.e. has signal
Another way from same link, but not marked as solution, is to check for network operator field as below
public static Boolean isMobileAvailable(Context appcontext) {
TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);
return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);
}
Try this:-
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
boitealerte(this.getString(R.string.alert),"getSystemService rend null");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
It will return true if the network is available.
I am using org.apache.http in my app to connect with internet but when I am running my app in Samsung galaxy duos y gt-1602, then it is not able to use Sim GPRS while it is running properly with WiFi.
Can anyone suggest me what could be the problem or possible solution.
Is there any lines of code related to Network connectivity type in your pgm?
Following if a simple method to detect connectivity.
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++) {
Log.d("Connectivity", "info:"+info[i]);//Here you can check network info if available
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.
My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.
The code
protected boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
} //end checkInterneConnection method
Thanks for your time.
Mel
You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable: http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29
This piece of code will check whether your device Internet conecction, If the signal is Poor it will show a Toast other wise not,
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = conMan.getActiveNetworkInfo();
if(Info == null){
Toast.makeText(RegisterActivity.this,"Network Connection Failed! ", Toast.LENGTH_SHORT).show();
}
You can try ping http://google.com or doing something like this to confirm it's ok to visit internet.
You should try this:
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)
m_context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() ==
NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
And to check only wifi is simpler:
private boolean isWifiConnected() {
int WIFI_STATE = wifi.getWifiState();
if(WIFI_STATE == WifiManager.WIFI_STATE_ENABLED)
return true;
return false;
}
This code will really test the internet connection:
public static boolean isInternetAvailable() {
try {
InetAddress address = InetAddress.getByName("google.com");
return address.isReachable(2000); //This really tests if the ip, given by the url, is reachable
//return !address.equals(""); //This just tests if the IP is available but it could be taken in a previous request when internet was available
} catch (Exception e) {
Log.d("Internet check", "Unable to reach the url: "+url);
}
return false;
}
I want to check the network connectivity in android application.so i inserted the following code
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
{
for (int i = 0; i < info.length; i++)
{
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
when i removed network cable in my computer the program crashed.but when i disable Airplane Mode in Emulator,it correctly shows "NETWORK NOT AVAILABLE".
How to we actually check?
Did you try it on actual device.
My inference from your post is, you are trying on an emulator.
And I think emulator is linked to the network interface of the system
Hence it could have failed.
Other change what I propose :
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info.getState() == NetworkInfo.State.CONNECTED )
return true
How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.
You can use this to determine whether you are connected:
final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); // ctx stands for the Context
final NetworkInfo mobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final NetworkInfo wifi = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI );
// Return true if connected, either in 3G or wi-fi
return ((mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) ||
(wifi != null && wifi.getState() == NetworkInfo.State.CONNECTED) );
}
This code requires the permissions ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE.
EDIT: public NetworkInfo getNetworkInfo (int networkType) has been deprecated in API 21. For API levels higher than 20 you can do as follows:
final Network[] networks = connectManager.getAllNetworks();
boolean connected = false;
for (int ctr = 0; !connected && ctr < networks.length; ++ctr) {
final NetworkInfo network = connectManager.getNetworkInfo(networks[ctr]);
final int netType = network.getType();
connected = (network.getState() == NetworkInfo.State.CONNECTED) &&
(
netType == ConnectivityManager.TYPE_MOBILE ||
netType == ConnectivityManager.TYPE_WIFI /*||
netType == ConnectivityManager.TYPE_WIMAX ||
netType == ConnectivityManager.TYPE_ETHERNET */ );
}
return connected;
I limited this code to Wi-Fi and 3G as per the OP's question but it's easy to extend it to newly added connection types such as Ethernet or Wi-Max.
Don't know why your app crashes, maybe this helps. Maybe try this method: (needs the application context)
public static boolean isInternetConnectionActive(Context context) {
NetworkInfo networkInfo = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isConnected()) {
return false;
}
return true;
}
then try to call
if(isInternetConnectionActive(getApplicationContext)) {
/* start Intent */
}
else {
/* show Toast */
}
hope this helps
see this post, I answered the same question
how to save request and resend in android app
You can also use the TelephonyManager to find out what the mobile network type is:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();
Possible network types are listed at http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29