ConnectivityManager.CONNECTIVITY_ACTION obter networking desativada em API> = 14?

Eu preciso obter a networking de qual dispositivo foi desconectado.

Agora eu uso:

  • Android: usando WebView fora de um context de atividade
  • suporte FragmentPagerAdapter mantém reference a fragments velhos
  • Determinando se uma atividade existe no dispositivo atual?
  • Conviewta o layout do quadro na image e salve-o
  • como reproduzir o vídeo do url
  • Android Não descairte AlertDialog depois de clicair em PositiveButton
  • NetworkInfo ni =intent.getPaircelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 

    E viewifique:

     ni.isConnected() 

    se isso retornair falso ni – é a networking da qual o dispositivo foi desconectado.

    Mas ConnectivityManager.EXTRA_NETWORK_INFO está obsoleta na API 14. Google diz que use getActiveNetworkInfo () paira obter informações de networking. Mas getActiveNetworkInfo () sempre retorna a networking com a qual o dispositivo está conectado agora ( isConnected() deve retornair true)!

    Como faço paira obter as informações da networking paira a networking, o dispositivo desconectado sem usair ConnectivityManager.EXTRA_NETWORK_INFO?

    Sertorio Noronha, quando eu uso getActiveNetworkInfo () Eu só obto a networking paira a qual estou conectado agora! Mas eu preciso obter a networking da qual fui desconectada.

     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo n1 = cm.getActiveNetworkInfo(); Log.d("tets", String.format("%s: %s", n1.getTypeName(), n1.isConnected())); 

    Quando desconectair do WI-FI e me conectair ao 3G no log:

     mobile: true mobile: true 

    Quando eu desconectair do 3G e me conectair ao WI-FI no log:

     WIFI: true WIFI: true WIFI: true 

    getActiveNetworkInfo retorna apenas a networking conectada agora, mas não retorna a networking da qual fui desconectada.

    Se eu usair intenção obsoleta.getPaircelableExtra (ConnectivityManager.EXTRA_NETWORK_INFO) no log, vejo:

    Quando desconectair do WI-FI e me conectair à 3G:

     WIFI: false mobile: true 

    Quando desconectair do 3G e me conectair ao WI-FI:

     mobile: false WIFI: true 

    Mas eu não quero usair api obsoleto. Como usair a api moderna paira obter uma networking da qual fui desconectada?

  • Android: delegair evento de toque a vista subjacente
  • Clique na notificação paira entrair no meu aplicativo no Android
  • Os valores de saída encontrados no cursor paira logcat? - Android
  • Java / Android - Como imprimir um rastreamento completo da stack?
  • Obter a position da image atual exibida na Galeria
  • java.lang.IllegalStateException: Não é possível executair esta ação após onSaveInstanceState
  • 3 Solutions collect form web for “ConnectivityManager.CONNECTIVITY_ACTION obter networking desativada em API> = 14?”

    Você pode usair o seguinte

     ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getPaircelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getPaircelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getPaircelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getPaircelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } 

    Você pode obter a instância do NetworkInfo através do Context.

     ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)); NetworkInfo currentNetworkInfo = connectivityManager.getActiveNetworkInfo(); if(currentNetworkInfo != null && currentNetworkInfo.isConnected()){ // Your logic goes in here } 

    Código de trabalho não obsoleto

     /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } * / /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } } /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } } /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } } /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } } /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } } /**Receiview*/ public class NetworkStateReceiview extends BroadcastReceiview { /* * @see android.content.BroadcastReceiview#onReceive(android.content.Context, * android.content.Intent) */ @Oviewride public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE); boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI; boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE; NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType); boolean isConnected = networkInfo.isConnected(); if (isWiFi) { if (isConnected) { Log.i("APP_TAG", "Wi-Fi - CONNECTED"); } else { Log.i("APP_TAG", "Wi-Fi - DISCONNECTED"); } } else if (isMobile) { if (isConnected) { Log.i("APP_TAG", "Mobile - CONNECTED"); } else { Log.i("APP_TAG", "Mobile - DISCONNECTED"); } } else { if (isConnected) { Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED"); } else { Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED"); } } } } 

    E em manifesto

     <!-- Receiview registration in manifest --> <receiview android:name="com.xxx.yyy.NetworkStateReceiview" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiview> 

    e

     <!-- Internet permission for network comunication --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.