Monday 15 September 2014

nullpointerexception - how to check internet connection in Google nexus 7 (android 4.2) -


I used to follow the code to check the Internet connection.

  public static boolean checkNetworkConnection (References) {boolean connected = true; ConnectivityManager connectivityManager = (ConnectivityManager) reference .getSystemService (Context.CONNECTIVITY_SERVICE); If (connectivityManager.getNetworkInfo (ConnectivityManager.TYPE_MOBILE) .getState () == android.net.NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo (ConnectivityManager.TYPE_WIFI) .getState () == android.net.NetworkInfo.State.CONNECTED ) {Connected = true; } And connected = false; }   

This code is working fine under the mobile but it is not working for Google Nexus 7 (Android 4.2).

When I test this code in Google Nexus 7 (Android 4.2). I got the error.

Tap Pointer Exception in Connection Manager

This work for me:

  Public stable boolean isInternetEnabled () {ConnectivityManager conMgr = (ConnectivityManager) YourApp.context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = conMgr.getActiveNetworkInfo (); If (netInfo! = Null & amp; amp; Net Infos. Connect or Orkknekting ()) return true; Second false return; }    

No comments:

Post a Comment