Reputation: 1266
I use the following code for checking my internet connection.
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected() && ni.isAvailable())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected() && ni.isAvailable())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
but it says that I have the internet connection connection either the internet connection is disconnected or connected. and this method is called in the middle of the onCreate() method and closes the application by the "back" button. Please help me on this.
Upvotes: 0
Views: 460
Reputation: 2145
Firstly add these these two permission code lines in your AndroidManifest.xml
file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Then in your java code:
//declaration
boolean net;
//onCreate
net = isOnline();
if (net == true) {
//perform internet related task
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(
AddSite.this);
alert.setMessage(
"No signal found or Internet connection is not connected.")
.setTitle("Error")
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method
// stub
}
}).show();
}
//method
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null;
// return cm.getActiveNetworkInfo().isConnected();
}
Upvotes: 1
Reputation: 6891
public boolean checkConnection()
{
//ARE WE CONNECTED TO THE NET
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (YourActivityClassName.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
Add this in the manifest file and then try.
Try with this. I used this in one of my project and it works fine for me.. Hope this will help you...
Upvotes: 0
Reputation: 448
Did you write the permissions in manifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If u r checking in emulator, U have to set the connection in settings.
Upvotes: 0
Reputation: 2243
If your goal is communicating with a specific server (or domain) then you can use
try {
InetAddress.getByName(SERVER).isReachable(CONNECTIVITY_TIMEOUT);
} catch (UnknownHostException e1) {
//unknown host
} catch (IOException e1) {
//IO exception
}
eg SERVER=stackoverflow.com; CONNECTIVITY_TIMOUT=1000; // 1 second
Upvotes: 0
Reputation: 157447
you can achieve this listening for Connectiviy broadcast intent. So create your broadcast receiver and in the onReceive
callback check for:
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
remember to filter the intent with android.net.conn.CONNECTIVITY_CHANGE
Edit: look the BroadcastReceiver doc
hope it helps
Upvotes: 1