I use an observer pattern to monitor when a network connection changes. The problem I am experiencing is that if the power saving mode is enabled on any device, returning from the background to the foreground of my application will instantly cause a โnetwork outageโ when checking connectivity. Due to the nature of the observer pattern, this change will cause my connectionless dialog, although after a millisecond the connection is restored.
The connection signal remains strong when you return to the application from the background, but for some reason, the power saving mode tricks the system into thinking that there is no connection when connected. How can I control this? Is there a way to ignore connection verification if power saving mode is active?
Here is my network observer class. I register receivers in my application and use the application life cycle to determine when I will listen to changes on the network and when I will not. Thanks in advance!
public class NetworkReceiver extends BroadcastReceiver { private static final String TAG = NetworkReceiver.class.getSimpleName(); private static final List<NetworkStatusObserver> mObserverList = new ArrayList<>(); private static boolean isNetworkConnected = true; @Override public void onReceive(Context context, Intent intent) { Logger.i(TAG, "onReceive() broadcast"); boolean disconnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); boolean isNetworkConnectedCurrent; if (disconnected) { isNetworkConnectedCurrent = false; } else { NetworkInfo networkInfo; if (Build.VERSION.SDK_INT < 17) { networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); } else { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); networkInfo = cm.getActiveNetworkInfo(); } isNetworkConnectedCurrent = !FrameworkUtils.checkIfNull(networkInfo) && networkInfo.isConnectedOrConnecting(); } if (isNetworkConnectedCurrent != isNetworkConnected) { isNetworkConnected = isNetworkConnectedCurrent; Logger.d(TAG, "NetworkStatus.onReceive - isNetworkConnected: " + isNetworkConnected); notifyObservers(isNetworkConnected); } } private void notifyObservers(Boolean isNetworkConnectedCurrent) { for (NetworkStatusObserver networkStatusObserver : mObserverList) { networkStatusObserver.notifyConnectionChange(isNetworkConnectedCurrent); } } public void addObserver(NetworkStatusObserver observer) { mObserverList.add(observer); } public void removeObserver(NetworkStatusObserver observer) { mObserverList.remove(observer); } public int getObserverSize() { return mObserverList.size(); } public boolean contains(NetworkStatusObserver observer) { return mObserverList.contains(observer); } public void printObserverList() { Logger.i(TAG, "===== PRINT OBSERVER LIST ===== "); for (int i = 0; i < mObserverList.size(); i++) { Logger.i(TAG, String.format("item(%d): %s", i, mObserverList.get(i).toString())); } } public interface NetworkStatusObserver { void notifyConnectionChange(boolean isConnected); } }
android networking broadcastreceiver
portfoliobuilder
source share