Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, April 18, 2016

Internet Connection

Internet Connection


i am using ConnectivityManager. i don't know what happen to my code it always return null if internet connected. plese help me.

public static boolean isOnline(Context ctx) {      // Toast.makeText(ctx,"get", Toast.LENGTH_LONG).show();      ConnectivityManager mManager = (ConnectivityManager) ctx              .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo mNetworkInfo = mManager.getActiveNetworkInfo();      if ((mNetworkInfo != null) && (mNetworkInfo.isConnected())) {          return true;      }      return false;      }              if (NetworkConnection.isOnline(TIENews.this)) {              new GetNewsDetailAsyncTask(TIENews.this, url, newsListView)                      .execute();          } else {              Log.e("tag", "netIno return null");              super.OpenWiFiDialog(getResources().getString(                      R.string.internet_error_msg));          }      }  

thanks dear. my code was running fine before today and app is live also. but i dont know what happen during from last days to current time in this code and failed to run. and i ma not getting problem

Answer by Toppers for Internet Connection


The code seems to be fine. i think the problem with context i.e the context is null either Internet Permission in Manifest.. so please check it.

Answer by Zied Rebhi for Internet Connection


This is how you check the network if it exists or not :

ConnectivityManager  

method 1

public boolean isConnected(){    boolean result=false;    ConnectivityManager cm = (ConnectivityManager) getApplicationContext()      .getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();    if (null != activeNetwork) {              if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)               {                result=true;               msg="You are connected to a WiFi Network";               }              if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)              {                  result=true;                msg="You are connected to a Mobile Network";                 }              }       else{     msg = "No internet Connectivity";   result=false;  }       return result;   }  

method 2

public  boolean isConnected() {    ConnectivityManager cm = (ConnectivityManager) getApplicationContext()      .getSystemService(Context.CONNECTIVITY_SERVICE);     // test     if (cm.getActiveNetworkInfo() != null       && cm.getActiveNetworkInfo().isAvailable()       && cm.getActiveNetworkInfo().isConnected()) {       msg="You are connected to a Network";       return true;     } else {        msg = "No internet Connectivity";        return false;     }    }  

Add this lines to your manifest :

         

Answer by Priyanka Minhas for Internet Connection


I can't find any bit of

error in your code. May be the context is null.

Or you can simply try this simple line of code to check the internet connectivity...

// by default - its false      private boolean isInternetConnected = false;    // Check the internet connectivity         isInternetConnected = Utilities.isInternetAvailable(MainActivity.this);    if(isInternetConnected){    // perform here  }    public static boolean isInternetAvailable(Context context) {          boolean haveConnectedWifi = false;          boolean haveConnectedMobile = false;          boolean connectionavailable = false;          ConnectivityManager cm = (ConnectivityManager) context                  .getSystemService(Context.CONNECTIVITY_SERVICE);          NetworkInfo[] netInfo = cm.getAllNetworkInfo();          NetworkInfo informationabtnet = cm.getActiveNetworkInfo();          for (NetworkInfo ni : netInfo) {              try {                  if (ni.getTypeName().equalsIgnoreCase("WIFI"))                     if (ni.isConnected())                         haveConnectedWifi = true;                  if (ni.getTypeName().equalsIgnoreCase("MOBILE"))                     if (ni.isConnected())                         haveConnectedMobile = true;                  if (informationabtnet.isAvailable()                         && informationabtnet.isConnected())                     connectionavailable = true;                } catch (Exception e) {                    System.out.println("Inside utils catch clause , exception is"                         + e.toString());                  e.printStackTrace();              }          }          return haveConnectedWifi || haveConnectedMobile;       }  

Also add the Internet Permission in your manifest.

        

Answer by Shahbaz Akhtar for Internet Connection


    public class ConnectionDetector {            private Context _context;            public ConnectionDetector(Context context) {              this._context = context;          }            public boolean isConnectingToInternet() {              ConnectivityManager connectivity = (ConnectivityManager) _context                      .getSystemService(Context.CONNECTIVITY_SERVICE);              if (connectivity != null) {                  NetworkInfo[] info = connectivity.getAllNetworkInfo();                  if (info != null)                      for (int i = 0; i < info.length; i++)                          if (info[i].getState() == NetworkInfo.State.CONNECTED) {                              return true;                          }                }              return false;    Use this code. Its working  

Answer by Haresh Chhelana for Internet Connection


Try this way,hope this will help you to solve your problem.

public boolean isNetwokReachable() {     ConnectivityManager connectivityManager = (ConnectivityManager) mSmartAndroidActivity.getSystemService(Context.CONNECTIVITY_SERVICE);     if (connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo() .isConnected()) {         return true;     } else {         return false;     }  }  

Add below required permission in AndroidManifest.xml :

        


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.