Check connection is Available
is there a way to find out if the device is connected to the Internet and then start activity else toast a message when click on the button.
Check connection is Available
is there a way to find out if the device is connected to the Internet and then start activity else toast a message when click on the button.
Have a look on Android's Developer pages, here is a sort example:
You need to have these two permissions in your AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Then you can do something like the following to check network connectivity:
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// start your activity here
} else {
// display your toast here
}
For a more extended example you can see here:
http://developer.android.com/training/basics/network-ops/connecting.html
if (isNetworkAvailable())
{
Network is here..write your code here
}
else
{
// Notify user they aren't connected
Toast.makeText(getApplicationContext(),"You aren't connected to the internet.", Toast.LENGTH_SHORT).show();
}
private boolean isNetworkAvailable()
{
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
use this method,if netwok available than it return "true", otherwise "false" of boolean type..