I\'m running an android app that displays a webview on specific URL, I want to check if the application server I\'m accessing is alive and that I can view HTML, if failed I
Using-BroadCastReceiver-Way-It's Efficient-to-implement&realistic
As you're saying that you need to check the specific server for alive() or not?. Then it depends upon your choice. But always try to be general.
To receive for Internet Connectivity dropped / up, listen for 'ConnectivityManager.CONNECTIVITY_ACTION' action.
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
getApplicationContext().registerReceiver(mNetworkStateIntentReceiver, filter);
Whenever there is a change in connectivity i.e it could be either data connection is connected or disconnected, you will receive this event as broadcast, so in onreceive of the broadcast receiver, please check for internetconnect connection and decide whether internet is up or down.
BroadcastReceiver mNetworkStateIntentReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
NetworkInfo info = intent.getParcelableExtra(
ConnectivityManager.EXTRA_NETWORK_INFO);
String typeName = info.getTypeName();
String subtypeName = info.getSubtypeName();
System.out.println("Network is up ******** "+typeName+":::"+subtypeName);
if( checkInternetConnection()== true )
{
"Decide your code logic here...."
}
}
}
}
private boolean checkInternetConnection()
{
ConnectivityManager conMgr = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&conMgr.getActiveNetworkInfo().isConnected())
{
return true;
}
else
{
return false;
}
}
Hope it may helps you somewhere!!