The power saving mode does not cause a network connection when the application returns from the background

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); } } /** * Lets all {@link NetworkStatusObserver}s know if the DEVICE is connected to a network. * * @param isNetworkConnectedCurrent */ private void notifyObservers(Boolean isNetworkConnectedCurrent) { for (NetworkStatusObserver networkStatusObserver : mObserverList) { networkStatusObserver.notifyConnectionChange(isNetworkConnectedCurrent); } } /** * Add observer to observer list * * @param observer */ public void addObserver(NetworkStatusObserver observer) { mObserverList.add(observer); } /** * Remove observer from observer list * * @param observer */ public void removeObserver(NetworkStatusObserver observer) { mObserverList.remove(observer); } /** * Retrieve observer list size * * @return */ public int getObserverSize() { return mObserverList.size(); } /** * Check if receiver is added to observer list * * @param observer * @return */ public boolean contains(NetworkStatusObserver observer) { return mObserverList.contains(observer); } /** * Method is used to print observer list */ 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())); } } /** * Interface for monitoring network status change */ public interface NetworkStatusObserver { void notifyConnectionChange(boolean isConnected); } } 
+7
android networking broadcastreceiver
source share

No one has answered this question yet.

See related questions:

386
Save / save / restore scroll position when returning to ListView
364
How to handle notification when an application is in the background in Firebase
362
How to determine when an Android app goes to the background and comes to the fore
282
When you launch the application in debug mode, the Android application
7
No connection with VPN?
4
getAllNetworkInfo () is deprecated in M, but its replacement has difference behavior
one
Power Saving Network Connection
one
Kill Network Service Discovery from AsyncTask when done without leaks
one
Broadcast receiver does not work when application is closed (from everywhere)
-2
Can someone show me how to read this JSON in the given url