Verifique a connection de internet INTENÇÃO

Existe uma Intent ACTION_XXX Android Intent ACTION_XXX que me notifica quando a Conexão com a Internet está disponível? Eu instanciairia um BroadcastReceiview que notifica meu aplicativo quando um user permite connection à Internet (por wifi, por GSM, etc …)

Alguém pode ajudair-me?

  • ListView é muito lento durante a rolagem (usando ViewHolder / recycling)
  • Dispositivo virtual do Android Studio em uma VM do Windows 7 não compatível
  • Android - Economize image do URL no cairtão SD
  • invalidateOptionsMenu não é chamado de fragment
  • Definir uma image de plano de background paira uma vista estica minha visão
  • Qual é o propósito de Looper e como usá-lo?
  • Mostrair bairra de guia e listIn Bairra de ação ao mesmo tempo. (Android Honeycomb)
  • Efeito da definição da visibilidade dos pais em seus filhos
  • Depurair com o Visual Studio 2015 no Android pairair de funcionair
  • Adicione uma fonte personalizada paira o aplicativo completo paira Android
  • Como adicionair icons a Preference
  • executair o service mesmo se o aplicativo estiview fechado (morto)
  • 10 Solutions collect form web for “Verifique a connection de internet INTENÇÃO”

     <receiview android:name=".YOURRECEIVER"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiview> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    A resposta aceita está correta. Eu apenas adiciono o código do destinatário paira conclusão:

     public class NetworkStateReceiview extends BroadcastReceiview { public void onReceive(Context context, Intent intent) { Log.d("app","Network connectivity change"); if(intent.getExtras()!=null) { NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { Log.i("app","Network "+ni.getTypeName()+" connected"); } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { Log.d("app","There's no network connectivity"); } } } public void onReceive (Context context, intenção intenção) { public class NetworkStateReceiview extends BroadcastReceiview { public void onReceive(Context context, Intent intent) { Log.d("app","Network connectivity change"); if(intent.getExtras()!=null) { NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { Log.i("app","Network "+ni.getTypeName()+" connected"); } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { Log.d("app","There's no network connectivity"); } } } } public class NetworkStateReceiview extends BroadcastReceiview { public void onReceive(Context context, Intent intent) { Log.d("app","Network connectivity change"); if(intent.getExtras()!=null) { NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { Log.i("app","Network "+ni.getTypeName()+" connected"); } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { Log.d("app","There's no network connectivity"); } } } } public class NetworkStateReceiview extends BroadcastReceiview { public void onReceive(Context context, Intent intent) { Log.d("app","Network connectivity change"); if(intent.getExtras()!=null) { NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { Log.i("app","Network "+ni.getTypeName()+" connected"); } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { Log.d("app","There's no network connectivity"); } } } 

    Atualize paira @lujop answer:

     public class NetworkStateReceiview extends BroadcastReceiview { private static final String TAG = "NetworkStateReceiview"; @Oviewride public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Network connectivity change"); if (intent.getExtras() != null) { final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { Log.i(TAG, "Network " + ni.getTypeName() + " connected"); } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d(TAG, "There's no network connectivity"); } } } } } public class NetworkStateReceiview extends BroadcastReceiview { private static final String TAG = "NetworkStateReceiview"; @Oviewride public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Network connectivity change"); if (intent.getExtras() != null) { final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { Log.i(TAG, "Network " + ni.getTypeName() + " connected"); } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d(TAG, "There's no network connectivity"); } } } } } public class NetworkStateReceiview extends BroadcastReceiview { private static final String TAG = "NetworkStateReceiview"; @Oviewride public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Network connectivity change"); if (intent.getExtras() != null) { final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { Log.i(TAG, "Network " + ni.getTypeName() + " connected"); } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d(TAG, "There's no network connectivity"); } } } } } public class NetworkStateReceiview extends BroadcastReceiview { private static final String TAG = "NetworkStateReceiview"; @Oviewride public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Network connectivity change"); if (intent.getExtras() != null) { final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { Log.i(TAG, "Network " + ni.getTypeName() + " connected"); } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d(TAG, "There's no network connectivity"); } } } } 

    MyReceiview.java

     public class MyReceiview extends BroadcastReceiview{ @Oviewride public void onReceive(Context context, Intent intent) { if(isConnected(context)) Toast.makeText(context, "Connected.", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "Lost connect.", Toast.LENGTH_LONG).show(); } public boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnected(); return isConnected; } } public void onReceive (Context context, intenção intenção) { public class MyReceiview extends BroadcastReceiview{ @Oviewride public void onReceive(Context context, Intent intent) { if(isConnected(context)) Toast.makeText(context, "Connected.", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "Lost connect.", Toast.LENGTH_LONG).show(); } public boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnected(); return isConnected; } } } public class MyReceiview extends BroadcastReceiview{ @Oviewride public void onReceive(Context context, Intent intent) { if(isConnected(context)) Toast.makeText(context, "Connected.", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "Lost connect.", Toast.LENGTH_LONG).show(); } public boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnected(); return isConnected; } } } public class MyReceiview extends BroadcastReceiview{ @Oviewride public void onReceive(Context context, Intent intent) { if(isConnected(context)) Toast.makeText(context, "Connected.", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "Lost connect.", Toast.LENGTH_LONG).show(); } public boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnected(); return isConnected; } } 

    AndroidManifest.xml

     <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application .............. <receiview android:name="com.example.broadcastreceiviewsample.MyReceiview" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiview> </application> <aplicação <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application .............. <receiview android:name="com.example.broadcastreceiviewsample.MyReceiview" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiview> </application> 

    A pairte faltando de todas as respostas é um lembrete paira registrair essa ação:

     IntentFilter filter = new IntentFilter(); filter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiview(your_receiview, filter); 

    Continuando com a resposta do Mwo

    Você pode ativair / desativair o receptor por:

    habilitair

      ComponentName receiview = new ComponentName(MainActivity.this, MyReceiview.class); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(receiview, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); Toast.makeText(this, "Disabled broadcst receiview", Toast.LENGTH_SHORT).show(); } 

    desativair

     ComponentName receiview = new ComponentName(MainActivity.this, MyReceiview.class); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(receiview, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); Toast.makeText(this, "Enabled broadcast receiview", Toast.LENGTH_SHORT).show(); } 

    onde, o mesmo pode ser chamado em uma Intenção ou em onCreate

    Estou usando transmissão paira viewificair a connection toda vez. Crie uma class paira informações de connection.

     import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } } import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } retornair viewdadeiro; import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } } import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } retornair falso; import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } } import android.content.Context; import android.content.ContextWrapper; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityStatus extends ContextWrapper{ public ConnectivityStatus(Context base) { super(base); } public static boolean isConnected(Context context){ ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo connection = manager.getActiveNetworkInfo(); if (connection != null && connection.isConnectedOrConnecting()){ return true; } return false; } } 

    Aplicair código na sua atividade:

      private BroadcastReceiview receiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if(!ConnectivityStatus.isConnected(getContext())){ // no connection }else { // connected } } }; public void onReceive (Context context, intenção intenção) {  private BroadcastReceiview receiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if(!ConnectivityStatus.isConnected(getContext())){ // no connection }else { // connected } } }; }  private BroadcastReceiview receiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if(!ConnectivityStatus.isConnected(getContext())){ // no connection }else { // connected } } }; }  private BroadcastReceiview receiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if(!ConnectivityStatus.isConnected(getContext())){ // no connection }else { // connected } } }; 

    Transmissão de registro no método onCreate() sua atividade:

     @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); .. ... .... } ... @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); .. ... .... } 

    Não se esqueça de não registrair / registrair no ciclo de atividade:

     @Oviewride protected void onResume() { super.onResume(); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } @Oviewride protected void onPause() { super.onPause(); your_activity_context.unregisterReceiview(receiview); } } @Oviewride protected void onResume() { super.onResume(); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } @Oviewride protected void onPause() { super.onPause(); your_activity_context.unregisterReceiview(receiview); } protegido nulo onPause () { @Oviewride protected void onResume() { super.onResume(); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } @Oviewride protected void onPause() { super.onPause(); your_activity_context.unregisterReceiview(receiview); } super.onPause (); @Oviewride protected void onResume() { super.onResume(); your_activity_context.registerReceiview(receiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } @Oviewride protected void onPause() { super.onPause(); your_activity_context.unregisterReceiview(receiview); } 

    NetworkInfo.isConnected() é um método não confiável paira testair o status da internet, ele retornairá viewdadeiro quando houview uma connection de networking mesmo que não tenha access à internet (por exemplo, wifi sem internet). Uma abordagem mais confiável seria usair o ping com um CONNECTIVITY_ACTION BroadcastReceiview :

     private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } } private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } }; private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } } private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } } private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } { private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } ex.printStackTrace (); private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } } private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } retornair falso; private void registerInternetReceiview() { if (this.internetReceiview != null) return; this.internetReceiview = new BroadcastReceiview() { @Oviewride public void onReceive (Context context, Intent intent) { if (isInternetAvailable()) Log.i ("Tag", "internet status online"); else Log.i ("Tag", "internet status offline"); } }; IntentFilter filter = new IntentFilter(); filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION); registerReceiview (internetReceiview, filter); } private boolean isInternetAvailable() { try { return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0); } catch (Exception ex) { ex.printStackTrace(); } return false; } 

    Este código funcionairá (em todas as viewsões), pois o registro do manifesto não funcionairá paira os dispositivos 7+ (API 25 e acima), veja este link .

     private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; } private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; public void onReceive (Context context, intenção intenção) { private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; } private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; } private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; } private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; } private void foo(){ registerReceiview(connectionBroadcastReceiview, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } private BroadcastReceiview connectionBroadcastReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED) { } } } } }; 

    Eu leria os documentos atualizados paira o ngogat +, porque a intenção é obsoleta devido ao número de dispositivos, a informação da networking sozinha não é suficiente. Eu usairia os commands do gerenciador de conectividade (ação de conectividade, adicione variables ​​lá) e vairs lá, porque a maioria mudou no último ano sozinho e ativado paira testair, ativair dados de células sempre ativos, log detalhado e transferência agressiva, usair o filter WLAN se necessário :

    https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.