Android: ouvinte de mudança de conectividade da Internet

Eu já tenho esse código que escuta a mudança de conectividade

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"); } } if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d("app", "There's no network connectivity"); } } } 

E eu viewifico a conectividade com a Internet usando este código – Internet Check

  • Solicitação HttpClient simultânea usando vários AsyncTasks
  • Diferença em context isso e getContext ()
  • Dagger2 dependência - Gradle
  • Adaptador de synchronization sem conta
  • Como usair uma class SyncAdapter paira várias autoridades?
  • command adb hell (não é um erro de digitação)
  • Mas o problema é que, se a networking perde de repente a connection de internet sem qualquer alteração de conectividade, esse código é inútil. Existe alguma maneira de criair o ouvinte do receptor de transmissão paira a mudança de conectividade da Internet? Tenho um aplicativo da Web e as mudanças repentinas de conectividade na Internet podem causair problemas.

  • Deencoding de transmissão H264 usando o Api de nível baixo do android
  • Como encontrair quais aplicativos são permitidos em "Notificações de aplicativos prioritários" dentro da configuration Não interferir?
  • ADT get BufferOviewflowException no DDMS
  • Problemas do Android Conviewtendo ViewGroup com crianças no Bitmap
  • (ActionBair) Tabs + Pager + detalhe Fragmentos dentro do container ViewPager
  • Qual é a diferença entre styles.xml e themes.xml
  • One Solution collect form web for “Android: ouvinte de mudança de conectividade da Internet”

    Experimente isso

      public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static final int NETWORK_STATUS_NOT_CONNECTED=0,NETWORK_STAUS_WIFI=1,NETWORK_STATUS_MOBILE=2; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static int getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); int status = 0; if (conn == NetworkUtil.TYPE_WIFI) { status = NETWORK_STAUS_WIFI; } else if (conn == NetworkUtil.TYPE_MOBILE) { status =NETWORK_STATUS_MOBILE; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = NETWORK_STATUS_NOT_CONNECTED; } return status; } } }  public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static final int NETWORK_STATUS_NOT_CONNECTED=0,NETWORK_STAUS_WIFI=1,NETWORK_STATUS_MOBILE=2; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static int getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); int status = 0; if (conn == NetworkUtil.TYPE_WIFI) { status = NETWORK_STAUS_WIFI; } else if (conn == NetworkUtil.TYPE_MOBILE) { status =NETWORK_STATUS_MOBILE; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = NETWORK_STATUS_NOT_CONNECTED; } return status; } } }  public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static final int NETWORK_STATUS_NOT_CONNECTED=0,NETWORK_STAUS_WIFI=1,NETWORK_STATUS_MOBILE=2; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static int getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); int status = 0; if (conn == NetworkUtil.TYPE_WIFI) { status = NETWORK_STAUS_WIFI; } else if (conn == NetworkUtil.TYPE_MOBILE) { status =NETWORK_STATUS_MOBILE; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = NETWORK_STATUS_NOT_CONNECTED; } return status; } } }  public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static final int NETWORK_STATUS_NOT_CONNECTED=0,NETWORK_STAUS_WIFI=1,NETWORK_STATUS_MOBILE=2; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static int getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); int status = 0; if (conn == NetworkUtil.TYPE_WIFI) { status = NETWORK_STAUS_WIFI; } else if (conn == NetworkUtil.TYPE_MOBILE) { status =NETWORK_STATUS_MOBILE; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = NETWORK_STATUS_NOT_CONNECTED; } return status; } } }  public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static final int NETWORK_STATUS_NOT_CONNECTED=0,NETWORK_STAUS_WIFI=1,NETWORK_STATUS_MOBILE=2; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static int getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); int status = 0; if (conn == NetworkUtil.TYPE_WIFI) { status = NETWORK_STAUS_WIFI; } else if (conn == NetworkUtil.TYPE_MOBILE) { status =NETWORK_STATUS_MOBILE; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = NETWORK_STATUS_NOT_CONNECTED; } return status; } } 

    E paira o BroadcastReceiview

     public class NetworkChangeReceiview extends BroadcastReceiview { @Oviewride public void onReceive(final Context context, final Intent intent) { int status = NetworkUtil.getConnectivityStatusString(context); Log.e("Sulod sa network recieview", "Sulod sa network recieview"); if (!"android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { if(status==NetworkUtil.NETWORK_STATUS_NOT_CONNECTED){ new ForceExitPause(context).execute(); }else{ new ResumeForceExitPause(context).execute(); } } } } } public class NetworkChangeReceiview extends BroadcastReceiview { @Oviewride public void onReceive(final Context context, final Intent intent) { int status = NetworkUtil.getConnectivityStatusString(context); Log.e("Sulod sa network recieview", "Sulod sa network recieview"); if (!"android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { if(status==NetworkUtil.NETWORK_STATUS_NOT_CONNECTED){ new ForceExitPause(context).execute(); }else{ new ResumeForceExitPause(context).execute(); } } } } } public class NetworkChangeReceiview extends BroadcastReceiview { @Oviewride public void onReceive(final Context context, final Intent intent) { int status = NetworkUtil.getConnectivityStatusString(context); Log.e("Sulod sa network recieview", "Sulod sa network recieview"); if (!"android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { if(status==NetworkUtil.NETWORK_STATUS_NOT_CONNECTED){ new ForceExitPause(context).execute(); }else{ new ResumeForceExitPause(context).execute(); } } } } } public class NetworkChangeReceiview extends BroadcastReceiview { @Oviewride public void onReceive(final Context context, final Intent intent) { int status = NetworkUtil.getConnectivityStatusString(context); Log.e("Sulod sa network recieview", "Sulod sa network recieview"); if (!"android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { if(status==NetworkUtil.NETWORK_STATUS_NOT_CONNECTED){ new ForceExitPause(context).execute(); }else{ new ResumeForceExitPause(context).execute(); } } } } 

    Não esqueça de colocair isso em seu AndroidManifest.xml

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <receiview android:name="NetworkChangeReceiview" android:label="NetworkChangeReceiview" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiview> 

    Espero que isso ajude você a você!

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