Code Monkey home page Code Monkey logo

backgrounddetector-android's Introduction

Download License: MIT TeamCity status

Background Detector

image

A simple component you can integrate into your Android app in order to detect if the app goes to background or foreground

This version of the library has no additional dependencies.

Usage

1. Without droid4me

You need the create a customn Application class for your application and register a listener that will listen to the activies life cycle throught the implementation of the ActivityLifecycleCallbacks interface.

public final class CustomApplication
    extends Application
    implements ActivityLifecycleCallbacks
{

  @Override
  public void onCreate()
  {
    super.onCreate();
    registerActivityLifecycleCallbacks(this);
  }

  @Override
  public void onTerminate()
  {
    super.onTerminate();
    unregisterActivityLifecycleCallbacks(this);
  }

  @Override
  public void onActivityCreated(Activity activity, Bundle savedInstanceState)
  {

  }

  @Override
  public void onActivityStarted(Activity activity)
  {

  }

  @Override
  public void onActivityResumed(Activity activity)
  {

  }

  @Override
  public void onActivityPaused(Activity activity)
  {

  }

  @Override
  public void onActivityStopped(Activity activity)
  {

  }

  @Override
  public void onActivitySaveInstanceState(Activity activity, Bundle outState)
  {

  }

  @Override
  public void onActivityDestroyed(Activity activity)
  {

  }
}

Do not forget to update your AndroidManifest.xml file in order to specify that tour application use a custom Application class :

<?xml version="1.0" encoding="utf-8"?>
<manifest 
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.smartnsoft.myapplication"
>
  <application
    android:name=".CustomApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
  >
    <!-- activities declaration -->
  </application>

</manifest>

Your custom Application class can also implement the OnVisibilityChangedListener to be notify when the application goes to background or foreground :

public final class CustomApplication
    extends Application
    implements ActivityLifecycleCallbacks, OnVisibilityChangedListener
{

  //...
 
  @Override
  public void onAppGoesToBackground(Context context)
  {

  }

  @Override
  public void onAppGoesToForeground(Context context)
  {

  }
}

Then, you have to declare a BackgroundDetectorHandler and notify it each time your application detects that an Activity is resumed or paused. This BackgroundDetectorHandler takes a BackgroundDetectorCallback with which one you can tune some parameters like :

  • the default visibility in order to be notified or not the first time the app goes to foreground ;
  • the OnVisibilityChangedListener.
public final class CustomApplication
    extends Application
    implements ActivityLifecycleCallbacks, OnVisibilityChangedListener
{

  private BackgroundDetectorHandler backgroundDetectorHandler;

  @Override
  public void onCreate()
  {
    super.onCreate();
    registerActivityLifecycleCallbacks(this);
    backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
  }

  //...

  @Override
  public void onActivityResumed(Activity activity)
  {
    backgroundDetectorHandler.onActivityResumed(activity);
  }

  @Override
  public void onActivityPaused(Activity activity)
  {
    backgroundDetectorHandler.onActivityPaused(activity);
  }
  
  @Override
  public void onAppGoesToBackground(Context context)
  {
    Log.d("CustomApplication", "onAppGoesToBackground");
  }

  @Override
  public void onAppGoesToForeground(Context context)
  {
    Log.d("CustomApplication", "onAppGoesToForeground");
  }
  
}

2. With droid4me

When you use the droid4me framework, you do not have to listen for the activities life cycle through the ActivityLifecycleCallbacks interface. In fact, you can directly use your ApplicationNameInterceptor class that should implements of the ActivityController.Interceptor interface.

In this class, you can directly declare a BackgroundDetectorHandler that takes a BackgroundDetectorCallback with which one you can tune some parameters like :

  • the default visibility in order to be notified or not the first time the app goes to foreground ;
  • the OnVisibilityChangedListener.

Note : In this exemple, the class directly implements the OnVisibilityChangedListener interface.

public final class ApplicationNameInterceptor
    implements ActivityController.Interceptor, OnVisibilityChangedListener
{

  private BackgroundDetectorHandler backgroundDetectorHandler;

  public ApplicationNameInterceptor()
  {
    backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
  }

  @Override
  public void onAppGoesToBackground(Context context)
  {

  }

  @Override
  public void onAppGoesToForeground(Context context)
  {

  }

  @SuppressWarnings("unchecked")
  @Override
  public void onLifeCycleEvent(final Activity activity, Object component, InterceptorEvent interceptorEvent)
  {
    if (component != null)
    {
      // It's a Fragment
      //...
    }
    else
    {
      // It's an Activity
      //...
    }
  }

}

Finally, you can notify your BackgroundDetectorHandler instance each time your interceptor detects that an Activity is resumed or paused :

public final class ApplicationNameInterceptor
    implements ActivityController.Interceptor, OnVisibilityChangedListener
{

  private BackgroundDetectorHandler backgroundDetectorHandler;

  public ApplicationNameInterceptor()
  {
    backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
  }

  @Override
  public void onAppGoesToBackground(Context context)
  {
    Log.d("ApplicationNameInterceptor", "onAppGoesToBackground");
  }

  @Override
  public void onAppGoesToForeground(Context context)
  {
    Log.d("ApplicationNameInterceptor", "onAppGoesToForeground");
  }

  @SuppressWarnings("unchecked")
  @Override
  public void onLifeCycleEvent(final Activity activity, Object component, InterceptorEvent interceptorEvent)
  {
    if (component != null)
    {
      // It's a Fragment
      //...
    }
    else
    {
      // It's an Activity
      if(interceptorEvent == InterceptorEvent.onStart)
      {
        backgroundDetectorHandler.onActivityResumed(activity);
      }

      if(interceptorEvent == InterceptorEvent.onStop)
      {
        backgroundDetectorHandler.onActivityPaused(activity);
      }
    }
  }

}

Download

To add Background Detector to your project, include the following in your app module build.gradle file:

implementation ("com.smartnsoft:backgrounddetector:${latest.version}")

More

Check this article (in french) if you want to learn more how the library works.

License

This library is available under the MIT license. See the LICENSE file for more info.

Author

This library was proudly made by Smart&Soft, Paris FRANCE

backgrounddetector-android's People

Contributors

ludovicroland avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.