com.samsung.android.sdk.healthdata

Class HealthDataObserver



  • public abstract class HealthDataObserver
    extends Object
    This class defines a health data observer for health data change.

    Adding an Observer

    You can add an observer for the specific health data type and get notification in onChange(String) if the designated health data is changed.

    In case that you add an observer for the data type that updates too frequently in short time like step count, it can send too many events to your application and it can bring degradation of your application's performance.
    Check that your application needs to update data with real-time from Samsung Health again before adding an observer.

    A health data observer can be defined as below.

       public class HealthDataObserverExample {
      
           // The state of connection
           private HealthDataStore mStore;
      
           public static final String APP_TAG = "MyApp";
      
           private final HealthDataObserver mObserver = new HealthDataObserver(null) {
      
               // Checks notification for changed health data
               @Override
               public void onChange(String dataTypeName) {
                   Log.d(APP_TAG, "Health data is changed.");
                   readChangedData(dataTypeName);
               }
           };

    When you add an observer for the specific health data type, specify the data type name as below. Only data type is allowed to be observed.

           public void start() {
               // Adds an observer for change of the weight
               HealthDataObserver.addObserver(mStore,
               HealthConstants.Weight.HEALTH_DATA_TYPE, mObserver);
           }

    The change notification is received in onChange(String) and you can check updated health data as below.

           private void readChangedData(String dataTypeName) {
               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
               HealthDataResolver.ReadRequest rdRequest = new HealthDataResolver.ReadRequest.Builder()
                   .setDataType(dataTypeName)
                   .build();
               try {
                   // Make an asynchronous request to read health data
                   resolver.read(rdRequest).setResultListener(mRdListener);
               } catch (Exception e) {
                   Log.d(APP_TAG, "HealthDataResolver.read() fails.");
               }
           }
      
           private final HealthResultHolder.ResultListener<HealthDataResolver.ReadResult> mRdListener =
               new HealthResultHolder.ResultListener<HealthDataResolver.ReadResult>() {
      
               @Override
               public void onResult(HealthDataResolver.ReadResult result) {
                   try {
                       Iterator<HealthData> iterator = result.iterator();
      
                       if (iterator.hasNext()) {
                           HealthData data = iterator.next();
                           // Check the result
                       }
                   } finally {
                       result.close();
                   }
               }
           };
       }

    Since:
    1.0.0
    • Constructor Detail

      • HealthDataObserver

        public HealthDataObserver(Handler handler)
        Creates a HealthDataObserver instance.
        Parameters:
        handler - the handler to run onChange(String). If it's null, onChange(String) event is run on binder thread which is invoked by Samsung Health.
        Since:
        1.0.0
    • Method Detail

      • onChange

        public abstract void onChange(String dataTypeName)
        Called if health data registered as an observer is changed.
        Parameters:
        dataTypeName - The data type name that health data is changed
        Since:
        1.0.0
      • addObserver

        public static void addObserver(HealthDataStore store,
                                       String dataTypeName,
                                       HealthDataObserver observer)
        Adds an observer that wants to follow data change for the given health data type.
        Parameters:
        store - The health data store for connection
        dataTypeName - The health data type name to follow change
        observer - An observer to receive notification if health data is changed
        Throws:
        IllegalArgumentException - If the data type or observer is invalid
        IllegalStateException - If the instance of health data store is invalid or a remote-invocation error occurs in the connection
        Since:
        1.0.0
      • removeObserver

        public static void removeObserver(HealthDataStore store,
                                          HealthDataObserver observer)
        Removes a registered observer.
        Parameters:
        store - health data store for connection
        observer - The added observer
        Throws:
        IllegalArgumentException - If the observer is invalid
        IllegalStateException - If the instance of health data store is invalid or a remote-invocation error occurs in the connection
        Since:
        1.0.0