互联网连接状态变化的Android事件
我正在制作一个应用程序,用户上传信息和文件到我的服务器有点频繁。 这是通过专门的上传服务在新线程中完成的。
我从这个线索知道
检测Android上是否有可用的Internet连接
您可以比较容易地检查是否有互联网连接。 您还可以获取socketTimeoutExceptions来检测Internet连接问题。 所有这一切都很好,并且可以在连接因任何原因而无法工作时轻松地caching我的上传内容。
我的问题是,我怎么知道什么时候重新尝试上传? 连接恢复时是否触发事件? 还是我坚持做一个睡眠的新线程,然后每隔30秒检查一次互联网连接?
任何想法将不胜感激!
Avtar
有一个连接变化的广播接收器。 看到这里
很老的post,但我想分享我的接收器
没有必要把你的手放在清单或其他无聊的资源:)
用法
您的活动:
/* * You need to implement NetworkStateReceiverListener. * This interface is described inside the NewtworkStateReceiver class */ public class MyActivity implements NetworkStateReceiverListener { /* ... */ private NetworkStateReceiver networkStateReceiver; }
在你的活动中:让接收者知道
public void onCreate(Bundle savedInstanceState) { /* ... */ networkStateReceiver = new NetworkStateReceiver(); networkStateReceiver.addListener(this); this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); } public void onDestroy() { super.onDestroy(); networkStateReceiver.removeListener(this); this.unregisterReceiver(networkStateReceiver); }
在您的活动中:实施所需的方法
@Override public void networkAvailable() { Log.d("tommydevall", "I'm in, baby!"); /* TODO: Your connection-oriented stuff here */ } @Override public void networkUnavailable() { Log.d("tommydevall", "I'm dancing with myself"); /* TODO: Your disconnection-oriented stuff here */ }
收件人
只需复制并粘贴到您的项目NetworkStateReceiver.java
public class NetworkStateReceiver extends BroadcastReceiver { protected Set<NetworkStateReceiverListener> listeners; protected Boolean connected; public NetworkStateReceiver() { listeners = new HashSet<NetworkStateReceiverListener>(); connected = null; } public void onReceive(Context context, Intent intent) { if(intent == null || intent.getExtras() == null) return; ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = manager.getActiveNetworkInfo(); if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) { connected = true; } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { connected = false; } notifyStateToAll(); } private void notifyStateToAll() { for(NetworkStateReceiverListener listener : listeners) notifyState(listener); } private void notifyState(NetworkStateReceiverListener listener) { if(connected == null || listener == null) return; if(connected == true) listener.networkAvailable(); else listener.networkUnavailable(); } public void addListener(NetworkStateReceiverListener l) { listeners.add(l); notifyState(l); } public void removeListener(NetworkStateReceiverListener l) { listeners.remove(l); } public interface NetworkStateReceiverListener { public void networkAvailable(); public void networkUnavailable(); } }
请享用 ;)
如果你只是想在连接改变的时候做一些简单的事情,那么有一个更简单的解决scheme。
在您的活动中,创build一个广播接收器:
private BroadcastReceiver networkStateReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = manager.getActiveNetworkInfo(); doSomethingOnNetworkChange(ni); } };
然后在onResume
和onPause
做注册:
@Override public void onResume() { super.onResume(); registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); } @Override public void onPause() { unregisterReceiver(networkStateReceiver); super.onPause(); }
不太清楚Tommaso的广播接收机发生了什么事情,但它不适合我,这是我的实施。 它只通知连接可用/不可用之间的变化。
我也注册在onResume()
和取消注册onPause()
。 否则就和上面一样。
public class NetworkStateReceiver extends BroadcastReceiver { private ConnectivityManager mManager; private List<NetworkStateReceiverListener> mListeners; private boolean mConnected; public NetworkStateReceiver(Context context) { mListeners = new ArrayList<NetworkStateReceiverListener>(); mManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); checkStateChanged(); } public void onReceive(Context context, Intent intent) { if (intent == null || intent.getExtras() == null) return; if (checkStateChanged()) notifyStateToAll(); } private boolean checkStateChanged() { boolean prev = mConnected; NetworkInfo activeNetwork = mManager.getActiveNetworkInfo(); mConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); return prev != mConnected; } private void notifyStateToAll() { for (NetworkStateReceiverListener listener : mListeners) { notifyState(listener); } } private void notifyState(NetworkStateReceiverListener listener) { if (listener != null) { if (mConnected) listener.onNetworkAvailable(); else listener.onNetworkUnavailable(); } } public void addListener(NetworkStateReceiverListener l) { mListeners.add(l); notifyState(l); } public void removeListener(NetworkStateReceiverListener l) { mListeners.remove(l); } public interface NetworkStateReceiverListener { public void onNetworkAvailable(); public void onNetworkUnavailable(); } }
Android Nougat&O(API 24+) – networking状态
@darnmason的答案(构造函数),使其在API 24 +工作。
public NetworkStateReceiver(Context context) { mListeners = new ArrayList<>(); mManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(NetworkStateReceiver.this, intentFilter); checkStateChanged(); }