com.samsung.android.sdk.healthdata

Class HealthDataStore



  • public class HealthDataStore
    extends Object
    This class provides connection to the health data store.

    App Manifest

    From Android 11, add the following element in your app's manifest. Otherwise, a connection with Samsung Health fails.

       <manifest . . . >
           <queries>
               <package android:name="com.sec.android.app.shealth" />
           </queries> 
       </manifest>

    Data Store Connection

    Health data in the health data store can be accessed after getting connection with the health data store.
    HealthDataStore requests to connect or disconnect with the health data store and its result can be checked with HealthDataStore.ConnectionListener.

    An example below shows how to request connection to the health data store and check the result.

       public class MainActivity extends Activity {
      
           // The state of connection
           private HealthDataStore mStore;
      
           private static final String APP_TAG = "MyApp";
      
           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
      
               connect();
           }
      
           @Override
           public boolean onCreateOptionsMenu(Menu menu) {
               // Inflate the menu; this adds items to the action bar if it is present.
               getMenuInflater().inflate(R.menu.main, menu);
               return true;
           }
      
           @Override
           public boolean onOptionsItemSelected(MenuItem item) {
               // Handle action bar item clicks here. The action bar will
               // automatically handle clicks on the Home/Up button, so long
               // as you specify a parent activity in AndroidManifest.xml.
               int id = item.getItemId();
               if (id == R.id.action_settings) {
                   return true;
               }
               return super.onOptionsItemSelected(item);
           }
      
           public HealthDataStore connect() {
               // Connect to the health data store
               mStore = new HealthDataStore(this, mCntListener);
               try {
                   mStore.connectService();
               } catch (Exception e) {
                   Log.d(APP_TAG, "Connection fails.");
                   e.printStackTrace();
               }
      
               return mStore;
           }
           
           private final HealthDataStore.ConnectionListener mCntListener =
                   new HealthDataStore.ConnectionListener() {
      
               @Override
               public void onConnected() {
                   Log.d(APP_TAG, "Health data service is connected.");
               }
      
               @Override
               public void onConnectionFailed(HealthConnectionErrorResult error) {
                   if (error.hasResolution()) {
                       // Resolve an error
                   } else {
                       Log.d(APP_TAG, "Health data service is not available.");
                   }
               }
      
               @Override
               public void onDisconnected() {
                   Log.d(APP_TAG, "Health data service is disconnected.");
               }
           };
       }

    Since:
    1.0.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class and Description
      static interface  HealthDataStore.ConnectionListener
      This interface provides the callback when the client is connected or disconnected with the health data store.