Android ConnectionBuddy


Source link: https://github.com/zplesac/android_connectionbuddy

![Method count](https://img.shields.io/badge/Methods count-188-e91e63.svg)

Android ConnectionBuddy

Provides a simple way of handling connectivity change events.

Usage

  1. Add the library as a dependency to your build.gradle
compile 'com.zplesac:connectionbuddy:version@aar'

Check the latest version here.

Versions prior to 1.0.5 were hosted on an older jCenter repository and aren't available anymore due to trademark issues.

  1. Initialize a ConnectionBuddy instance in your Application class. You'll also need to provide a global configuration by defining ConnectionBuddyConfiguration object.
public class SampleApp extends Application {

@Override
  public void onCreate() {

super.onCreate();

 ConnectionBuddyConfiguration networkInspectorConfiguration = new ConnectionBuddyConfiguration.Builder(this).build();

 ConnectionBuddy.getInstance().init(networkInspectorConfiguration);

  
}
 
}

All options in ConnectionBuddyConfiguration.Builder are optional. Use only those you really want to customize.

  1. Make your activites (or BaseActivity) extend ConnectionBuddyActivity, and react to connectivity change events in onConnectionChange(ConnectivityEvent event) callback method:
  @Override
public void onConnectionChange(ConnectivityEvent event) {

 if(event.getState() == ConnectivityState.CONNECTED){

  // device has active internet connection

 
}

 else{

 // there is no active internet connection on this device

 
}

}

If you don't want to extend ConnectionBuddyActivity, you can use manual configuration:

  • Register to connectivity change events in the onStart() method of your activity:
 @Override  protected void onStart() {

super.onStart();

ConnectionBuddy.getInstance().registerForConnectivityEvents(this, this);
 
}
 
  • Unregister from connectivity change events in the onStop() method of your activity:
  @Override
protected void onStop() {

 super.onStop();

 ConnectionBuddy.getInstance().unregisterFromConnectivityEvents(this);

}
 
  • Clear the stored connectivity state for your activity/fragment if it was restored from a saved instance state (in order to always have the latest connectivity state). Add to your onCreate() method the following line of code:
  @Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

...

if(savedInstanceState != null){

ConnectionBuddyCache.clearInternetConnection(this);

  
}

 
}

ConnectivityEvent also holds some additional information:

  • ConnectivityType enum, which defines the network connection type currently available on the user's device
  • ConnectivityStrength enum, which describes signal strength of the network connection.

ConnectionBuddy configuration

You can customize the default ConnectionBuddy configuration by providing your own configuration. Following values can be changed:

1. registerForWiFiChanges(boolean shouldRegister)

A Boolean value which defines whether we should register for WiFi network changes. The default value is set to true.

2. registerForMobileNetworkChanges(boolean shouldRegister)

A Boolean value which defines whether we should register for mobile network changes. The default value is set to true.

3. setMinimumSignalStrength(ConnectivityStrength minimumSignalStrength)

Defines the minimum signal strength for which the callback listener should be notified. The default value is set to ConnectivityStrength.POOR.

4. setNotifyImmediately(boolean shouldNotify)

A Boolean value which defines whether we want to notify the listener about the current network connection state immediately after the listener has been registered. The default value is set to true.

5. notifyOnlyReliableEvents(boolean shouldNotify)

A Boolean value which defines whether we want to use reliable network events. If we have an active internet connection, it will try to execute a test network request to determine whether a user is capable of any network operation. The default value is set to false.

Advanced usage with MVP pattern

ConnectionBuddy also provides ConnectivityPresenter which can be used as a base presenter for registering to connectivity change events. A more detailed example can be found here.

Backward compatibility

As of version 1.2.0, ConnectionBuddy can be used with your apps on devices all the way back to Android 2.3 (API 10). It should also work on devices with API 8-9, but that's not tested.

Changelog

Changelog is available in the releases tab.

For versions prior to 2.0.0, here.

Contributing

Feedback and code contributions are very much welcome. Just make a pull request with a short description of your changes. By making contributions to this project you give permission for your code to be used under the same license.

Resources

Pan

A light weight MV* framework to build android reusable UI components.

rog

Rog is an object generator designed for android test. It can create objects and set random values to their fields.

Spark is a simple Android library that takes a series of x,y points at any scale and draws them as a sparkline chart.

It's a framework for building Android UI in Kotlin code by using the concept of virtual views and reactive data flow, the goal is to reduce boilerplate while retaining the same Android layout constructs.

EasyIntro is the flexible, easy to use, all in one app intro library for your Android project.

A quick adapter library for ListView, GridView, RecyclerView, ViewPager and ExpandableListView on android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes