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

HallScheme is a lightweight simple library for creating rectangle halls.

An android custom view which allows you to have an arc style-menu on your pages.

RxJava style camera API for android, it is based on android.hardware.camera.

A simple viewpager indicator that uses arrows on the left and right. You can navigate viewpager by clicking arrows and customize them as well.

This library is used to handle location requests and maintains the flow with permission checks in marshmallow.

EdittextWithTag allows to manage tags using spaces.

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