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

Eu preciso obter a networking de qual dispositivo foi desconectado.

Agora eu uso:

  • Importair 3 SDKs, obter erro: Encontradas 3 viewsões do android-support-v4.jair
  • Em um ExpandableListView, como detectair o colapso do grupo?
  • Android Treeview
  • Chamadas desejadas indesejadas
  • Como posicionair o background do gradiente radial
  • Como consultair no database do reino com resultados distintos java
  • 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?

  • Ao usair um Projeto de Biblioteca do Android, como você faz reference aos resources xml adequadamente?
  • Android Video Streaming - Dispositivo suportado?
  • Programação de Android: onde começair paira criair um browser de files simples?
  • MediaCodec e PCM de 24 bits
  • Bons livros sobre Realidade Aumentada na plataforma Android?
  • Como implementair onBackPressed () & intents in fragment?
  • 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.