Check Whether Connected To The Internet Or Not Before Loading Webview?
I created a webview app in android i need to implement the condition i.e. if the internet or wifi is available means it will proceed to open the weblink. If the internet or WIFI is
Solution 1:
try this
connection detector
package com.likith.conectiondetector;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class connectiondetector
{
private Context _context;
/********************************************************************************/
public connectiondetector(Context context)
{
this._context = context;
}
/********************************************************************************/
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;
}
}
To use
connectiondetector cd= newconnectiondetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
if(isInternetPresent)
{
//Internet is connected
}
else
{
// Internet is not connected
}
Solution 2:
Create this method inside your activity:
publicbooleanisConnected(Context context){
ConnectivityManagercm= (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfoactiveNetwork= cm.getActiveNetworkInfo();
booleanisConnected= activeNetwork != null &&
activeNetwork.isConnected(); // isConnectedOrConnecting()return isConnected;
}
publicbooleanisWifi(Context context){
ConnectivityManagercm= (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfoactiveNetwork= cm.getActiveNetworkInfo();
booleanisWiFi= activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
return isWiFi;
}
To get the status whether connected:
// checkif (isConnected(this)) {
if (isWifi(this)) {
// connected to wifi
}else{
// connected to mobile network
}
}else{
// no network available
}
Post a Comment for "Check Whether Connected To The Internet Or Not Before Loading Webview?"