You could use the same approach to listen to any status, but this example includes network connectivity specifics too (ConnectionChangeReceiver
and AndroidUtils.isConnected
).
Last active
November 23, 2022 18:16
-
-
Save jonikarppinen/6578deb5c1615e03a6627e18f6c2ac3c to your computer and use it in GitHub Desktop.
Example of listening to connectivity status in Android and reacting to going offline. (Subscription, Observable, Observer, PublishSubject are from RxJava.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AndroidUtils { | |
public static boolean isConnected(Context context) { | |
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = manager.getActiveNetworkInfo(); | |
return activeNetwork != null && activeNetwork.isConnected(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Superclass for all activities | |
public abstract class BaseActivity extends Activity { | |
private static final String TAG = BaseActivity.class.getSimpleName(); | |
private Subscription onlineSubscription; | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
onlineSubscription = NetworkManager.getInstance().onlineSignal().subscribe(new GoOfflineObserver()); | |
if (!AndroidUtils.isConnected(this)) { | |
// Launch offline alert | |
} | |
} | |
@Override | |
protected void onPause() { | |
// When we're in the background, we do NOT want network status changes to bring our app to foreground again | |
if (onlineSubscription != null) { | |
onlineSubscription.unsubscribe(); | |
} | |
super.onPause(); | |
} | |
private class GoOfflineObserver implements rx.Observer<Boolean> { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable throwable) { | |
Log.e(TAG, "Error with onlineSignal", throwable); | |
} | |
@Override | |
public void onNext(Boolean online) { | |
if (!online) { | |
// Launch offline alert | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* onReceive is invoked when connection is lost or regained. | |
* | |
* This needs to be configured in AndroidManifest.xml, inside <application> element: | |
* | |
<receiver | |
android:name="com.company.product.network.ConnectionChangeReceiver" | |
android:label="NetworkConnection"> | |
<intent-filter> | |
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> | |
</intent-filter> | |
</receiver> | |
*/ | |
public class ConnectionChangeReceiver extends BroadcastReceiver { | |
private static final String TAG = ConnectionChangeReceiver.class.getSimpleName(); | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
boolean online = AndroidUtils.isConnected(context); | |
NetworkManager.getInstance().onlineObserver().onNext(online); | |
Log.i(TAG, String.format("Switching to %s mode", online ? "ONLINE" : "OFFLINE")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Class with all of the app's network code... Here omitting everything except onlineSubject | |
public class NetworkManager { | |
private PublishSubject<Boolean> onlineSubject = PublishSubject.create(); | |
public Observable<Boolean> onlineSignal() { | |
return onlineSubject; | |
} | |
Observer<Boolean> onlineObserver() { | |
return onlineSubject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment