Reputation: 863
i have developed one application in that i have to check whether net is connected or not i use following code but its not working. can any one resolve my problem. my code is
public class AbcActivity extends Activity {
Button b;
private static final String tag = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
netCheckin();
}
});
}
private void netCheckin() {
try {
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
Log.d(tag, "Net avail:"
+ nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(), "net on", Toast.LENGTH_LONG).show();
Log.d(tag, "Network available:true");
} else {
Toast.makeText(getApplicationContext(), "net OFF", Toast.LENGTH_LONG).show();
Log.d(tag, "Network available:false");
}
} catch (Exception e) {
Log.e("error",""+e);
}
}
}
i have give the android:name="android.permission.ACCESS_NETWORK_STATE"/> in my manifest file in above code my problem is when net is enable or disable it gives same output as "net on" (toast message).
Upvotes: 1
Views: 1108
Reputation: 4503
// check internet connectivity
public static boolean isNetworkAvailable(Context context) {
boolean networkStatus;
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
networkStatus = (activeNetworkInfo != null && activeNetworkInfo.isConnected()) ? true : false;
} catch (Exception e) {
e.printStackTrace();
DinotaLogger.log(e, Level.SEVERE);
networkStatus = false;
}
return networkStatus;
}
Please make sure to put in manifest file. If you are using an emulator to check this, press F8 to disable network access. This works fine with android 2.3.3 emulator.
Upvotes: 1
Reputation: 11571
At the top of your onCreate method , use
if(!checkInternetConnection()){
// network connnectivity error , show some dialog here and exit from the app
}
where
private boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
Log.v("", "Internet Connection Not Present");
return false;
}
}
Hope this wil help you.
Upvotes: 1
Reputation: 5562
Just try the below code so that u can check whether there is internet connected or not. This code is working for me.Also u can check if the internet is connected through mobile then it is in roaming or not.
int typeOfConnection = 0;
//Gets the system service of the connectivity.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = connectivityManager.getAllNetworkInfo();
//Checks whether the connectivity type is WIFI or MOBILE.
for (NetworkInfo networkInfo : netInfo)
{
if (networkInfo.getTypeName().equalsIgnoreCase("WIFI"))
if (networkInfo.isConnected()){
typeOfConnection = 1;
Toast.makeText(getApplicationContext(), "net on", Toast.LENGTH_LONG).show();
}
if (networkInfo.getTypeName().equalsIgnoreCase("MOBILE"))
if (networkInfo.isConnected()){
typeOfConnection = 2;
Toast.makeText(getApplicationContext(), "net on", Toast.LENGTH_LONG).show();
}
}
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(typeOfConnection== 2 && telephonyManager.isNetworkRoaming()){
typeOfConnection = 3;
}
if(typeOfConnection == 0){
Toast.makeText(getApplicationContext(), "net OFF", Toast.LENGTH_LONG).show();
}
All the best
Upvotes: 1
Reputation: 10295
I have done this in my application. To really check for net connection, try to ping or DNS resolve www.google.com :
@Waqas, that question doesn't deal with the situation of connected to router, but no Internet access because the ConnectivityManager
doesn't distinguish between Network and Internet connection.
Upvotes: 1