Aim: To Check internet Connection status/Type in Android.
Here is some example code to check the network status .
void chkConnectionStatus()
{
ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
}
This snippet requires the following permissions in AndroidManifest.xml file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Please let me know if any.
thanks for sharing, really work for me...^_^
ReplyDeleteIs it working for android sdk 2.2? could you help to provide permission code for AndroidMainfest.xml. Thanks, your blog really helpful for me.
ReplyDeleteThis comment has been removed by the author.
DeleteNeed to provide permission for AndroidMainfest.xml
Delete1. android:name="android.permission.ACCESS_NETWORK_STATE"
2. android:name="android.permission.ACCESS_WIFI_STATE"
this is great.This was really helpful in my project.
ReplyDeleteHowever I wish to know if there is anyway to modify this code so that only selected applications can access the internet through 3G while other applications that are not selected will not be able to use 3G data connection.
This is the last part of my project and I have been looking for a solution and I have not found any. only this was the closest thing to a solution.
I hope to get your opinion on my problem.
doesnot work
ReplyDeletei mean nothing display
any suggestions ! plz
This comment has been removed by the author.
ReplyDelete@Ola What exactly is not working or displaying?
ReplyDeleteThanks for the nice tutorial Rama... Even this
ReplyDeletehttp://www.compiletimeerror.com/2013/06/check-internet-connection-in-android.html might help, have a look...
thanks rama mohan
ReplyDeletethanks,
ReplyDeletecan u send me source code.