Android ConnectionBuddy

Additional

Language
Java
Version
v1.1.0 (Mar 17, 2016)
Created
Sep 1, 2015
Updated
Mar 20, 2016
Owner
Zeljko Plesac (zplesac)
Contributor
Zeljko Plesac (zplesac)
1
Activity
Badge
Generate
Download
ZIP

Commercial

Build Status License JCenter Method count Android Arsenal

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'

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

2) 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.

3) 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.

Changelog

Changelog is available 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.

To get this project into your build

Information:
  • It may be not the official build, but possibility to simplify access to some artifacts.
  • The project may be also unavailable on JitPack.io. If it is the case, you can open a corresponding issue in the project to resolve this problem.

Step 1. Select your build system:

Step 2. Add the specific repository to your build file:


Step 3. Add the dependency in your build file (do not forget to specify the correct qualifier, usually 'aar'):


That's it!

Information:
  • StackOverflow service is used to find questions and answers.
  • Results are close to the actual, but not completely accurate or exact. It depends on the uniqueness of the project name.
  • You can also ask a question, but remember how to do it correctly.

Loading...

Wait a little bit, it could take some time.