Problema de viewificação de conectividade de internet do Android

Eu sou novo no desenvolvimento do Android e trabalhando em um aplicativo Android que exige que o telefone seja conectado à internet, através de Wifi, EDGE ou 3G.

Este é o código que estou usando paira viewificair se uma connection à internet está disponível

public static boolean isConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo().isConnectedOrConnecting(); } 

Eu também estabeleci essas permissions no file de manifesto

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Isso funciona bem no emulador executando a viewsão 1.5 do Android quando o 3G está habilitado, mas ele trava quando desativair a connection 3G. O meu aplicativo lança uma exception de ponteiro nulo quando eu chamo isConnectedOrConnecting (). O mesmo acontece no meu HTC Desire executando o Android 2.1.

Espero que alguém conheça a solução paira isso.

Desde já, obrigado!

5 Solutions collect form web for “Problema de viewificação de conectividade de internet do Android”

Se o acidente estiview diretamente na sua linha:

 return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 

então isso significa que getActiveNetworkInfo() returnu null , porque não existe uma networking ativa – nesse caso, seu método isConnected() deve retornair false .

Eu escrevi esse método paira lidair com isso:

 public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni!=null && ni.isAvailable() && ni.isConnected()) { return true; } else { return false; } } retornair viewdadeiro; public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni!=null && ni.isAvailable() && ni.isConnected()) { return true; } else { return false; } } retornair falso; public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni!=null && ni.isAvailable() && ni.isConnected()) { return true; } else { return false; } } } public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni!=null && ni.isAvailable() && ni.isConnected()) { return true; } else { return false; } } 

Uma maneira de fazer isso, acho …

Paira viewificair a internet está lá ou não, pode ser viewificado apenas no dispositivo … No emulador, pode não funcionair … Tenho o seguinte código e está funcionando 100% no dispositivo Android ….. 🙂

 @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } retornair viewdadeiro; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } retornair falso; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.txt); b = checkInternetConnection(); if(b!=true) { tv.setText("net is not dr......."); } else { tv.setText("net is dr......."); } } //Check weather Internet connection is available or not public boolean checkInternetConnection() { final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) { return true; } else { System.out.println("Internet Connection Not Present"); return false; } } 

}

Você usou esse trecho.

 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; } } { 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; } } { 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; } } retornair viewdadeiro; 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; } } } 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; } } 

use isso paira detemina se conected paira wifi / 3g:

 is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected(); isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected(); network = is3g||isWifi; 

e isso permite wifi você mesmo:

 WifiManager wifiManager = (WifiManager) MainWindowYuval.this.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.